I use Log4j with the RollingFileAppender
to create a log rotation based on size.
How can I configure it to log to each file for a certain amount of time before rotating?
For example, so that each log file contains one hour of logs, rotating at the top of each hour?
I configure Log4j programatically in Java using a Properties
object (as opposed to a log4j.properties
file)
Best Answer
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:Or for your programmatic configuration:
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.