Search code examples
javaspringlogginglogback

logback-spring.xml won't append file name


This is my first time using logging in spring and I was given a logback-spring.xml to use and tweak. This is what my current logback-spring.xml looks like:

 <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
    <include 
     resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="logs/my_file_name.log}"/>
    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}</file>
        <rollingPolicy 
         class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
            <maxHistory>10</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
        </rollingPolicy>
    </appender>
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
 </configuration>

I have also added the following in my application.properties:

logging.config=config/logback-spring.xml

When I ran the program, the logs directory was automatically created. However the log present was named my_file_name.log instead of my_file_name.log.2017-10-25.gz as I need and as my logback-spring.xml specifies.

Why is this happening? Is it because of an error in the logback-spring.xml or because I need to specify something else in my application.properites or add something in my pom.xml? Or will the log name automatically change as I run this program again on different days?


Solution

  • On startup Logback will write to the file defined by appender/file and when the rolling policy kicks in Logback will copy the current log file to the file suggested by appender/rollingPolicy/fileNamePattern.

    In your case the rolling policy will kick in when the day rolls (this is what your pattern - %d{yyyy-MM-dd} - means). So every day up to day 10 Logback will roll the current log file and on day 10 it will roll the current log file and discard the oldest archived file thereby retaining only maxHistory archived files.

    Plenty more detail in the docs.

    BTW, your logback-spring.xml does seem to have an error in it, I suspect this ...

    <property name="LOG_FILE" value="logs/my_file_name.log}"/>
    

    ... should be:

    <property name="LOG_FILE" value="logs/my_file_name.log"/>