I use RollingFileAppender
of log4j 1.2.16
, which rolls log files, when they reach a certain size. Now I would like to roll log files daily and when they reach a certain size. Thus there will be one or more log files per day.
For example,
myapp.log myapp-17.12.2013.log myapp-16.12.2012.log myapp-16.12.2012.1.log myapp-16.12.2012.2.log
Is there an off-the-shelf appender, which does it already?
There are indeed two options:
Keep in mind that both options use file renames. Consider this carefully if there's another script automatically moving these files. File rename is risky when two processes deal with the same file.
My suggestion is to directly write to immutable log file name in the pattern: myapp-{dd.MM.yyyy}.{X}.log. That way "rolling" is simply closing one file and opening a new one. No renames. No background threads.
The quick answer is "no". Looking at log4j's javadoc: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/FileAppender.html
There are only two out-of-the-box file appenders: DailyRollingFileAppender and RollingFileAppender (and the first one is not recommended because it has synchronization issues).
To achieve what you want, you should create your own appender, extending RollingFileAppender and modifying it to roll the file if the day changes. The modification would be in method:
You can see its source here: http://www.docjar.com/html/api/org/apache/log4j/RollingFileAppender.java.html (line 274).
You just need to copy and paste the code and change the if calling rollOver to suit your needs.
Below configuration xml will do the job: JAR required: log4j-rolling-appender-20150607-2059