How to configure a log4j file appender which rolls the log file every 15 minutes

前端 未结 5 2066
孤独总比滥情好
孤独总比滥情好 2020-12-06 13:01

I understand that i can use a DailyRollingFileAppender to roll the log file every month, day, half-day, hour or minute. But how can i configure log4j to roll the log file ev

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 13:20

    With logback, you can subclass the RollingFileAppender.

    public class FifteenMinuteAppender extends RollingFileAppender
    {
       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();
          }
       }
    }
    

    Then use that appender as your appender with a rolling policy

    
      yourfile.csv
      
        
        yourfile-%d{yyyyMMdd-HHmm}.csv
      
      
        %d{YYYYMMDD-HHmmSS-ZZ};${HOSTNAME};%thread;%msg%n
      
    
    

    More details here

提交回复
热议问题