[logback-user] Is 5 minute file rollover possible?

Andreas Kruthoff andreas.kruthoff at nexustelecom.com
Tue Apr 24 10:01:58 CEST 2012


On 04/12/2012 02:11 PM, Andreas Kruthoff wrote:
> Is it possible to configure logback to rollover every 5 minutes, or
> every 15 minutes? How would I configure this?
>
> thanks for any suggestions
>
> -andreas
>
> This email and any attachment may contain confidential information which is intended for use only by the addressee(s) named above. If you received this email by mistake, please notify the sender immediately, and delete the email from your system. You are prohibited from copying, disseminating or otherwise using the email or any attachment.
>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
>

The following solution worked for me:

1. logback.xml, write all data to a file, 1-minute rollover.

...
<appender name="DATA-FILE" class="MyMessageAppender">
   <file>file.csv</file>
   <encoder>
     <pattern>%msg</pattern>
   </encoder>
   <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
     <fileNamePattern>file-%d{yyyyMMdd-HHmm}.csv</fileNamePattern>
     <cleanHistoryOnStart>true</cleanHistoryOnStart>
     <maxHistory>10</maxHistory>
   </rollingPolicy>
</appender>

<logger name="DATA" level="INFO" additivity="false">
   <appender-ref ref="DATA-FILE"/>
</logger>
...

2. MyMessageAppender, (real) rollover i.e. every 5 minutes.

public class MyMessageAppender<E> extends RollingFileAppender<E> {
   private int interval = 0; // minutes
   @Override
   public void rollover() {
     if (this.interval % 5 == 0) {
       super.rollover();
       this.interval = 0;
     }
     this.interval++;
   }
}

That's it, file.csv is rolled over every 5 minutes.


cheers

-andreas

--
I can't influence text below here.

This email and any attachment may contain confidential information which is intended for use only by the addressee(s) named above. If you received this email by mistake, please notify the sender immediately, and delete the email from your system. You are prohibited from copying, disseminating or otherwise using the email or any attachment.



More information about the Logback-user mailing list