2017

May

2016

September

August

June

2015

September

August

June

January

2014

December

July

April

March

February

2013

December

August

July

March

2012

December

August

Rolling logs every 15 mins with logback 04 Aug 2015

Want to create rolling logs every 15 mins with logback ?

Idea was adapted from Andreas Kruthoff post, original post

#Create a new appender

This is a fifteen minutes appender but it is easy to modify … You have to understand that the appender is called only when something is logged. So if nothing is logged for 10 minutes your appender will not be called for 10 minutes. Usually on real projects this is not an issue as a tons of logs are generated every minute…

So the idea is just to call the rollover method only when a period of 15min has been reached.

public class FifteenMinuteAppender<E> extends RollingFileAppender<E>
{
   private static long start = System.currentTimeMillis(); // minutes
   private int rollOverTimeInMinutes = 15;

   @Override
   public void rollover()
   {
      long currentTime = System.currentTimeMillis();
      int maxIntervalSinceLastLoggingInMillis = rollOverTimeInMinutes * 60 * 1000;

      if ((currentTime - start) >= maxIntervalSinceLastLoggingInMillis)
      {
         super.rollover();
         start = System.currentTimeMillis();
      }
   }
}

#Setup logback

Use your new appender in your logback configuration file.

 <appender class="com.your.package.FifteenMinuteAppender" name="YOUR_APPENDER">
    <file>yourfile.csv</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>yourfile-%d{yyyyMMdd-HHmm}.csv</fileNamePattern>
    </rollingPolicy>
    <encoder>
      <pattern>%d{YYYYMMDD-HHmmSS-ZZ};${HOSTNAME};%thread;%msg%n</pattern>
    </encoder>
  </appender>
comments powered by Disqus
This site was designed using jekyll, bootstrap, fontawesome, google analytics, disqus and is hosted on github. All for free so thank you!