Search code examples
javalogback

logback: Correct file name pattern for weekly rolling


I've tried the following rollingPolicy, but the log file didn't roll:

    <rollingPolicy
      class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>log-%d{yyyy-ww}.log.gz</fileNamePattern>
      <maxHistory>26</maxHistory>
    </rollingPolicy>

The fileNamePattern correspondes to the file definition of the appender.

Is my file name pattern wrong?


Solution

  • Shame on me! As so often, the problem often lies between the ears. In my project, I moved the log files to another directory. I changed the file tag of the appender but not the fileNamePattern of the rolling policy.

    To avoid such mistakes, I suggest to work consistently with variables! Example:

    <property name="log_path" value="/var/log/app-name"/>
    <property name="log_name" value="myLog"/>
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <property name="app_log" value="${log_path}/${log_name}"/>
        <file>${app_log}.log</file>
        <encoder>
            <pattern>%d [%t] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${app_log}.%d{yyyy-MM}.log.gz</fileNamePattern>
            <maxHistory>12</maxHistory>
        </rollingPolicy>
    </appender>