简体   繁体   中英

How to force logback to create a new file on rollover

I'm trying to achieve with logback the same thing that currently works with log4j - a size and time base rolling file appender. Upon rolling a new file should be created with the current timestamp but logback still writes to the old file.

I'm using something like:

<timestamp key="bySecond" datePattern="yyyyMMddkkmmss"/> 

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
  <file>${logback.PREFIX}-${bySecond}.log</file> 
  <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
    <fileNamePattern>${logback.PREFIX}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> 
    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 
        <maxFileSize>1GB</maxFileSize> 
    </timeBasedFileNamingAndTriggeringPolicy> 
  </rollingPolicy> 

Wanted behaviour:

  • application starts with my-20131206105823.log
  • a rolling event occurs (1GB or midnight)
  • the my-2013-12-06.0.log.gz file is created
  • the application resumes logging in my-20131207000001.log

Current behavior:

  • application starts with my-20131206105823.log
  • a rolling event occurs (1GB or midnight)
  • the my-2013-12-06.0.log.gz file is created
  • the application resumes logging in my-20131206105823.log

It would be nice if I would also find out how to have this done only on the day rollover and not on the size, but this could be enough.

Java 1.7 with logback-1.0.9

I am not sure but let me try to resolve it. Once the rolling event occurred, the content of "my-20131206105823.log" file get copied to "my-2013-12-06.0.log.gz" and you will again get the new file with the same old name i,e "my-20131206105823.log".

<file>${logback.PREFIX}-${bySecond}.log</file>

$bySecond get assigned just once when you start your application, It won't change on every rollback. Timestamp is just a variable. Check here

Also check this one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM