The Spring Boot docs have the following sample logging file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Can you help me understand the line ${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}
? What are the -
for?
Let's define a global environment variable called JAVA_HOME with the value “C:\Program Files\Java\jdk-11.0. 14”. To use this variable in Spring Boot's application.properties, we need to surround it with braces: java.home=${JAVA_HOME} We can also use the System properties in the same way.
Environment is an interface representing the environment in which the current application is running. It can be use to get profiles and properties of the application environment. In this sample case, we have a JAVA_HOME environment variable defined. This is the project structure of the Spring Boot application.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
Like Bash, Logback uses :-
as its default value operator. The line in question is setting the LOG_FILE
property:
LOG_FILE
is already set, use thatLOG_PATH
is set, use that suffixed with spring.log
LOG_TEMP
is set, use that suffixed with /spring.log
java.io.tmpdir
is set, use that suffixed with /spring.log
/tmp/spring.log
It has nothing to do with Spring.
Logback XML configuration itself has such kind of placeholder handling to replace the placeholders with variable. The syntax for placeholder in logback is ${VARNAME}
, and if you want default value if VARNAME
is not present, you can do it by ${VARNAME:-DEFAULT}
(ref: Logback Configuration). Yes, a :-
, follow by the default value.
Then what you quote is easy to understand:
${LOG_FILE:- }
${LOG_PATH:- }spring.log
${LOG_TEMP:- }/
${java.io.tmpdir:- }
/tmp
(Do you actually need explanation on what the above means?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With