Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j. Rollover each hour, zip daily

Can I configure log4j to rollover each hour, then compress all the daily log-files into one zip (so that zip contains 24 log-files).

Ideally I'd like to zip files only for those days which are one week old and earlier. But that's another part of the question.

like image 674
Alec Avatar asked Aug 08 '13 16:08

Alec


1 Answers

You probably want to use a DailyRollingFileAppender. To roll them hourly, for example, you'd use a DatePattern of '.'yyyy-MM-dd-HH. For a log4j.properties file:

log4j.appender.myAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myAppender.DatePattern='.'yyyy-MM-dd-HH
...

Or for your programmatic configuration:

DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setDatePattern("'.'yyyy-MM-dd-HH");

Logger root = Logger.getRootLogger();
root.addAppender(appender);

Unfortunately, using a DailyRollingFileAppender means that you can't limit the file size - this could be problematic if you have tons of logs in the given rolled period.

To compress take a look to: compress-log4j-files

like image 137
Khinsu Avatar answered Nov 17 '22 09:11

Khinsu