logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hachey, Philip" <Philip.Hac...@nrc-cnrc.gc.ca>
Subject How to configure log4j2 to rollover with time stamp on startup? (OR: feature request)
Date Mon, 14 May 2018 17:47:59 GMT
Hello.  This is a question about how to achieve a particular result with log4j2, but I expect
may instead be a *feature request*.

I have a Java application that runs for a few minutes each hour via a cron job (java -jar...).
It runs in its own JVM, *not* in a continuously running environment like Tomcat. I am using
Log4j 2.11 to do logging. Within the application, I have a particular logger with specific
rollover requirements:

1. Log to "rolling.log"
2. At the end of each day (or on the first logging event of a new day), rolling.log should
be rolled over to rolling-yyyy-MM-dd.log.gz and new logging events added to a fresh rolling.log.

I cannot get this to work.  It appears to me that time-based rollovers are only ever triggered
if the application is running at the time the rollover would happen.

For an example of how log4j2 handles things currently, suppose this order of events for a
logger configured to rollover daily.

23:30 -- cronjob runs application for 10 minutes, logging to rolling.log
00:00 -- new day; rollover would happen here but application is not running
00:30 -- cronjob runs application for 10 minutes, still logging to rolling.log and no rollover
happens

Result: both days of application log messages are logged to the same file: rolling.log, without
any rollover.


Feature Request (assuming there is not already a way do this): Allow time-based rollover to
trigger if, upon application execution, it is noted that the time a rollover should have happened
was after the previous application execution.

E.g.:
23:30 -- cronjob runs application for 10 minutes, logging to rolling.log
00:00 -- new day; rollover would happen here but application is not running
00:30 -- cronjob runs application for 10 minutes; at start of execution (or first log message),
log4j2 notes that a rollover should have happened and so rolls over the existing rolling.log
and adds all new log messages to a new rolling.log file.

Hint: to do this, log4j will likely need to record a timestamp of the last log message or
when the application previously terminated, then read this time stamp on application start
(or first log message).

Note: I originally posted this question on Stack Overflow and additional details, including
test code and my attempts at log4j2 configuration, may be found there:
https://stackoverflow.com/questions/48874164/how-to-configure-log4j2-to-rollover-with-time-stamp-on-startup

Thank you,
Philip
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message