SpringBoot with LogBack creating LOG_PATH_IS_UNDEFINED folder

Davi Alves picture Davi Alves · Aug 11, 2014 · Viewed 49.5k times · Source

I am using SpringBoot with LogBack and using the below configuration in my yml file:

logging:
    path: C:/var/log/pincode

The logging.path Spring Environment Variable is transferred to the LOG_PATH Environment variable and the log file is placed at the correct place, but there is also a directory called LOG_PATH_IS_UNDEFINED created in the root directory of my project.

This seems to be caused by the different phase used by SpringBoot to configure LogBack with its Environment variables.

17:29:21,325 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:29:21,337 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern LOG_PATH_IS_UNDEFINED/catalina.out.%d{yyyy-MM-dd} for the active file
17:29:21,340 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'LOG_PATH_IS_UNDEFINED/catalina.out.%d{yyyy-MM-dd}'.
17:29:21,340 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
17:29:21,343 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Aug 11 17:24:07 BRT 2014
17:29:21,346 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - Active log file name: LOG_PATH_IS_UNDEFINED/catalina.out
17:29:21,346 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - File property is set to [LOG_PATH_IS_UNDEFINED/catalina.out]
...
17:29:21,358 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

And then after that it start configuring logback again but this time using the path i set:

17:29:21,672 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
17:29:21,673 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - No compression will be used
17:29:21,673 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern C:/var/log/pincode//catalina.out.%d{yyyy-MM-dd} for the active file
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'C:/var/log/pincode//catalina.out.%d{yyyy-MM-dd}'.
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
17:29:21,674 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Aug 11 17:29:21 BRT 2014
17:29:21,674 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - Active log file name: C:/var/log/pincode//catalina.out
17:29:21,674 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[serverConsole] - File property is set to [C:/var/log/pincode//catalina.out]
...
17:29:21,685 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.

My logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<include resource="org/springframework/boot/logging/logback/basic.xml" />
<property name="FILE_LOG_PATTERN"
    value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex" />

<appender name="serverConsole"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <Append>true</Append>
    <File>${LOG_PATH}/catalina.out</File>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/catalina.out.%d{yyyy-MM-dd}
        </fileNamePattern>
        <maxHistory>15</maxHistory>
    </rollingPolicy>
</appender>

<!-- Plain Text Rolling Appender -->
<appender name="server"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <Append>true</Append>
    <File>${LOG_PATH}/pincode.log</File>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>INFO</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/pincode.log.%d{yyyy-MM-dd}
        </fileNamePattern>
        <maxHistory>15</maxHistory>
    </rollingPolicy>
</appender>

<!-- Plain Text Rolling Appender -->
<appender name="server-error"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <Append>true</Append>
    <File>${LOG_PATH}/pincode-error.log</File>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${LOG_PATH}/pincode-error.log.%d{yyyy-MM-dd}
        </fileNamePattern>
        <maxHistory>15</maxHistory>
    </rollingPolicy>
</appender>

<logger name="com.app" level="INFO">
    <appender-ref ref="server" />
    <appender-ref ref="server-error" />
</logger>

<root level="INFO">
    <appender-ref ref="serverConsole" />
</root> 

If I remove my logback.xml file from the project it doesn't create the folder, so somewhere Spring is loading the xml before parsing the yml?

How can I avoid Logback to create this LOG_PATH_IS_UNDEFINED directory?

Answer

Igor Tytar picture Igor Tytar · Dec 3, 2015

In your case LOG_PATH is not defined on startup. You should use ${LOG_PATH:-.} instead , See .

But if you define logging.path in your application.properties you will see two log files in . and in ${logging.path} directory.

Spring container set LOG_PATH after Logback initialization... Logback is not supported lazy file creation as far as I know. In this case you should use logback-spring.xml instead logback.xml.