Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explain this Spring environment variable resolution?

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?

like image 795
Abhijit Sarkar Avatar asked Jun 01 '16 18:06

Abhijit Sarkar


People also ask

What is environment variable in Spring?

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.

What is the use of environment in Spring?

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.

What is the @configuration in Spring boot?

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.


2 Answers

Like Bash, Logback uses :- as its default value operator. The line in question is setting the LOG_FILE property:

  • If LOG_FILE is already set, use that
  • Otherwise, if LOG_PATH is set, use that suffixed with spring.log
  • Otherwise, if LOG_TEMP is set, use that suffixed with /spring.log
  • Otherwise, if java.io.tmpdir is set, use that suffixed with /spring.log
  • Otherwise use /tmp/spring.log
like image 173
Andy Wilkinson Avatar answered Oct 18 '22 22:10

Andy Wilkinson


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?)

like image 32
Adrian Shum Avatar answered Oct 18 '22 22:10

Adrian Shum