Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get log4j to roll files based on date and size?

Tags:

log4j

So log4j comes with two existing log rollers: RollingFileAppender, and DailyRollingFileAppender. Has anyone heard of an appender that does both of what the former do?

I need an appender that will roll log files based on filesize, but also append the current date to it.

I've been thinking about creating my own appender, but if there is already one that has been created, why not save the time and use that one?

like image 854
Jose Avatar asked Apr 27 '09 19:04

Jose


People also ask

What is rolling file in log4j?

Java Logging. Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

How do you create a new log file for each time the application runs log4j?

File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

How do I change the log4j configuration at runtime?

Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact. For this to work, I usually add the first appender from code (and not with a config file).


3 Answers

I know this post is a bit late but you could try out the TimeAndSizeRollingAppender. It's freely available under the Apache 2.0 license, download it from www.simonsite.org.uk.

like image 42
Simon Avatar answered Sep 26 '22 02:09

Simon


Looks like you want a mix of the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html and the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/RollingFileAppender.html.

You'll have to code by yourself. The good news is: you'll just have "merge" those classes functionality, no "low level" new code required.

like image 66
razenha Avatar answered Sep 23 '22 02:09

razenha


The Log4J Extras from the "companions" project provide an array of policies for rolling, including what you're looking for.

APIDoc: http://logging.apache.org/log4j/extras/apidocs/index.html Homepage: http://logging.apache.org/log4j/extras/

like image 41
Florian Thiel Avatar answered Sep 24 '22 02:09

Florian Thiel