What determines when a log4j TimeBasedRollingPolicy rolls over?
Asked Answered
P

2

6

I'm setting up a TimeBasedRollingPolicy from Log4J Extras and I am not clear what tells the policy when to roll over. The API is not explicit so I'm just making inferences. It sounds like it's the last element in the FileNamePattern that determines the frequency.

Take this example from the log4j Wiki:

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <!-- The active file to log to -->
    <param name="file" value="/applogs/myportal/portal.log" />
    <param name="append" value="true" />
    <param name="encoding" value="UTF-8" />

    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
    <!-- The file to roll to, this is a fairly intelligent parameter, if the file
         ends in .gz, it gzips it, based on the date stamp it rolls at that time, 
         default is yyyy-MM-dd, (rolls at midnight)
    -->
        <param name="FileNamePattern" value="/applogs/myportal/portal.%d.log.gz" />
    </rollingPolicy>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
    </layout>
</appender>

Am I to assume that because the pattern ends in dd, the policy is to roll when that changes? Same with an example in the API, a pattern of yyyy-MM means the file should roll when MM changes?

Thanks!

Paul

Procephalic answered 25/3, 2011 at 15:16 Comment(0)
P
5

Well, I'd have to double check, but I'd say whenever the String produced by formatting the current date with the format string changes, the file is rolled. This means: if you format the date using "yyyy-MM-dd" the result would change every day. This would also happen with "dd" only, BUT you'd get the same filename every month, thus the files are either overwritten, be appended to or the rolling fails because the file already exists (not sure which is true, depends on what the appender does, I guess in this case logs will be appended, except maybe for the gzip way).

Edit:

Example: if you have mylog.%d{dd}.log, the resulting log file for today (2011-03-27) have the name mylog.25.log (due to formatting new Date() when logging) and would append messages to that file. Tomorrow, the now used file would have the name mylog.26.log. In April 25th you'd again get filename `mylog.25.log thus all logs from that day would be appended to the file which already contains logs from March 25th.

Penninite answered 25/3, 2011 at 15:24 Comment(1)
You're absolutely correct. I don't know why I didn't do this before but I checked the source code for TimeBasedRollingPolicy and in the logic for the rollover is the comment "if file names haven't changed, no rollover". Thanks!Procephalic
I
2

I got this working. Below is the code for log4j. You only need to add log4j-extras dependencies in pom.xml and use below code: The below code rolls at every minutes and creates .zip file.

<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">    
    <param name="FileNamePattern" value="/opt/app/srdotcom/logs/portal.%d{yyyy-MM-dd-HH-mm}.log.zip" />
</rollingPolicy>

<layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{YYYY-MM-dd HH:mm:ss:SSS z}| %c{2}| %m%n" />
</layout>

Maven Dependencies:

  <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>apache-log4j-extras</artifactId>
        <version>1.2.17</version>
    </dependency>
Intorsion answered 6/7, 2017 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.