Mike Viens
Mike Viens

Reputation: 2507

Incorrect rolling of log file using Logback

I am using Logback to create a daily rolling log file. It does rename the existing log file and creates a new one, however it is not doing it correctly (or I am telling it something incorrectly). Below is the logback.xml file I am using:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <timestamp key="today" datePattern="yyyyMMdd"/>

    <appender name="ScreenAppender" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <pattern>%date{HH:mm:ss.SSS} %-5level %logger{20} [%file:%line] - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="UpdateAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>true</append>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>info</level>
        </filter>
        <file>${CATALINA_HOME}/logs/update-server.log</file>
        <encoder>
            <pattern>%date{HH:mm:ss.SSS} %-5level %logger{40} [%file:%line] - %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>update-server.%d{yyyy-MM-dd}.zip</fileNamePattern>
            <maxHistory>180</maxHistory>
        </rollingPolicy>
    </appender>

    <root level="debug">
        <appender-ref ref="ScreenAppender"/>
        <appender-ref ref="UpdateAppender"/>
    </root>
</configuration>

The log file does get created as update-server.log. And when the day rolls over, the next generated message for logging does result in that log file being renamed, but not as I expect it to. The resulting name is update-server.log1161295506996864.tmp. And, the file itself is not ZIPped, just renamed.

I would expect the file to be named update-server.20120103.log and to have that file ZIPped. Does anyone know why?

As a second (minor) issue, I would like the original log file to be called update-server-YYYYMMDD.log, but when I tried to do that, the log file created stays the same and at midnight, the file is overwritten. I used the line below, but it appears that Logback does not increment the ${today} value:

<file>${CATALINA_HOME}/logs/update-server-${today}.log</file>

Upvotes: 1

Views: 3221

Answers (1)

SunilJ
SunilJ

Reputation: 101

Note the difference between the log file paths in

<file>${CATALINA_HOME}/logs/update-server.log</file>

and

<fileNamePattern>update-server.%d{yyyy-MM-dd}.zip</fileNamePattern>

Update your <fileNamePattern> to

<fileNamePattern>${CATALINA_HOME}/logs/update-server.%d{yyyy-MM-dd}.zip</fileNamePattern>

I had the same issue.

Upvotes: 0

Related Questions