I am trying to use an environment variable ($mode) in my logbook-spring.xml as below but logback is unable to read it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<property name="LOG_PATH" value="../logs/${mode}"/>
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive"/>
<timestamp key="timestamp-by-second" datePattern="yyyyMMdd'T'HHmmss"/>
<timestamp key="timestamp-by-day" datePattern="yyyyMMdd"/>
<appender name="Trace-Appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/emrTrace_${mode}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/emrTrace_${mode}.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date{HH:mm:ss.SSS} [%t] %msg%n</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
</appender>
<logger name="com.itreatmd.emr.Debug" level="finest" additivity="false">
<appender-ref ref="Trace-Appender"/>
</logger>
</configuration>
I run my executable jar as:
java -jar EmrServices-1.0-SNAPSHOT.jar --mode=prod
The property "LOG_PATH" is being set to "logs/mode_IS_UNDEFINED"
As such, when I iterate through my environment variables in the program, I do see "mode" among the variables. Which means that the application is aware of the variable. But apparently, logback is not.
I am using Spring 2.1.10. I didn't see this problem in much older versions of Springboot (1.3)
Edit: I am thinking that the reason for this is that logback is initialized before Spring, but I have no idea on what I can do about it.