D.Mey
D.Mey

Reputation: 13

Logback creates a file once and writes in this file even though it is from a bygone day

I want to have a new File every Day it is triggered apparently it just appends the old File no matter what day it is. Here is my logback.xml.

<timestamp key="byDay" datePattern="yyyy-MM-dd"/>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">

<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
   <file>/opt/tomcat/logs/log-${byDay}.log</file>
   <append>true</append>
   <immediateFlush>true</immediateFlush>
  <encoder>
   <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
   </encoder>
</appender>
<root level="DEBUG">
    <appender-ref ref="FILE" />
</root>

Upvotes: 1

Views: 1679

Answers (1)

riskop
riskop

Reputation: 1787

You have multiple problems in your config.

  • you don't need timestamp element, as @csenga noted
  • you need RollingFileAppender instead of FileAppender
  • < file... > should have no date pattern, that is the constant filename prefix

Corrected config (note that I set up the rolling to happen every minute):

<configuration scan="true" debug="true">
    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/opt/tomcat/logs/log.log</file>
        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>/opt/tomcat/logs/log_%d{yyyy-MM-dd_HHmm}.log
            </FileNamePattern>
        </rollingPolicy>
        <immediateFlush>true</immediateFlush>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="FILE" />
    </root>
</configuration>

resulting files:

riskop@riskop:/opt/tomcat/logs$ ls -l
total 24
-rw-rw-r-- 1 riskop riskop 13640 febr  15 14:30 log_2018-02-15_1430.log
-rw-rw-r-- 1 riskop riskop   310 febr  15 14:31 log_2018-02-15_1431.log
-rw-rw-r-- 1 riskop riskop   155 febr  15 14:37 log.log

If you have a look on logback's own logging then you will see that the rollback is logged:

14:37:20,673 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Elapsed period: Thu Feb 15 14:31:01 CET 2018
14:37:20,673 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [/opt/tomcat/logs/log.log] to [/opt/tomcat/logs/log_2018-02-15_1431.log]

If you omit the < file > parameter from the config, then the current file will be named according to the datetime pattern. That is not the usual config IMHO, because in that case you won't see what is the current file at first glance.

Upvotes: 1

Related Questions