Rolling logs by both size and time
Asked Answered
H

4

10

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?

Homologue answered 18/12, 2012 at 15:18 Comment(1)
#3824477Spaulding
T
8

There are indeed two options:

  1. use LogBack with its size and time triggering policy: http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
  2. there is TimeAndSizeRollingAppender for Log4J from here: http://www.simonsite.org.uk/

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.

Thalassic answered 7/1, 2013 at 18:45 Comment(0)
V
4

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:

 protected void subAppend(LoggingEvent event)

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.

Verduzco answered 18/12, 2012 at 15:35 Comment(0)
M
3

Below configuration xml will do the job: JAR required: log4j-rolling-appender-20150607-2059

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
    xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="file"
        class="uk.org.simonsite.log4j.appender.TimeAndSizeRollingAppender">
        <param name="File" value="D:\\App.log" />
        <param name="Threshold" value="DEBUG" />
        <param name="DatePattern" value=".yyyy-MM-dd" />
        <param name="MaxFileSize" value="1KB" />
        <param name="MaxRollFileCount" value="100" />
        <param name="ScavengeInterval" value="30000" />
        <param name="BufferedIO" value="false" />
        <param name="CompressionAlgorithm" value="GZ" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="file" />
    </root>

</log4j:configuration>
Mediatize answered 15/4, 2016 at 8:18 Comment(0)
Q
1

Log4j2 and logback are preferable but if you want log4j-v1:

Log4j-v1-extras has it: (org.apache.log4j.rolling.RollingFileAppender)

<appender name="roll-by-time-and-size" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="ActiveFileName" value="log4j/roll-by-time-and-size/app.log" />
        <param name="FileNamePattern" value="log4j/roll-by-time-and-size/app.%d{HH-mm}.%i.log.gz" />
    </rollingPolicy>
    <triggeringPolicy
        class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="MaxFileSize" value="100" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
    </layout>
</appender>

Source: https://www.baeldung.com/java-logging-rolling-file-appenders#5-rolling-based-on-size-and-time

Note: There is no old-file-deletion after X days. A cron job could be used to do that. If you don't want a cron job but want deletion, I'd recommend going with the log4j-v1 basic size-based RollingFileAppender or log4j-extras SizeBasedTriggeringPolicy (see baeldung article example).

Note: The "%i" in FileNamePattern is required for time + size to both work. It adds a large long-int (ticks?) to all backed-up log filenames. This is different from logback for which "%i" becomes 0,1,2,..

Log4j-extras dependency:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>apache-log4j-extras</artifactId>
    <version>1.2.17</version>
</dependency>
Quyenr answered 15/9, 2023 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.