Search code examples
javaxmlspringlogbackslf4j

Logback - Where do XML documents get their variables injected


I'm working on a Spring Boot project and I've seen logging done with slf4j and logback. The following is a logback-spring.xml I've seen that logs errors to the console in addition to a file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>

    <appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}-%d{yyyyMMddd}.%i</fileNamePattern>
            <maxFileSize>10MB</maxFileSize>
            <totalSizeCap>10GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="ROLLING"/>
        <appender-ref ref="STDOUT"/>
    </root>

</configuration>

My question is, where do things like ${CONSOLE_LOG_PATTERN} and ${LOG_FILE} get their actual data from at compile/run time or whenever this is used? I'm curious because ${LOG_FILE}-%d{yyyyMMddd}.%i usually leaves me with logs named like LOG_FILE_IS_UNDEFINED-201707012.0 which leads me to the obvious conclusion that LOG_FILE isn't defined anywhere, and I would like to remedy this.

I've seen resources like this, but they don't seem to provide enough details on the inner-workings of how the values are injected.


Solution

  • Spring boot comes with a default configuration for logback. The jar has embedded the xml configuration files for the different appenders. You can have a look to them at GitHub project: Default spring-boot logback configuration

    You can override that configuration including your own xml configuration files. Configure Logback for logging

    You are getting undefined file because you have probably not defined the property logging.file