Exact configuration for moving RollingFileAppender with top of the hour rollover policy from log4j to log4j2?

744 views Asked by At

So I have a DailyRollingFileAppender in log4j. We are in the process of moving from log4j to log4j2. Here is the log4j XML that describes our DailyRollingFileAppender setting:

<appender name="appender_1" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="/mnt/analytics/logs/others/analytics_.log"/>
        <!--  Rollover at the top of every hour. -->
        <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mmz}|%m%n"/>
        </layout>
    </appender>

As you can see, currently our logs rollover at the top of every hour. What is the exact configuration for the same format, i.e. rollover logs at the top of every hour, in log4j2?

I know the corresponding class in log4j2 is RollingFile, but what is the configuration that specifies the above rollover policy?

1

There are 1 answers

0
alan7678 On BEST ANSWER
<RollingFile name="appender_1" fileName="/mnt/analytics/logs/others/analytics_.log"
  filePattern="/mnt/analytics/logs/others/analytics_.log.%d{yyyy-MM-dd-HH}">
  <PatternLayout pattern="%d{yyyy-MM-dd HH:mmz}|%m%n"/>
  <Policies>        
    <TimeBasedTriggeringPolicy interval="1"/>
  </Policies>
  <DefaultRolloverStrategy max="24"/>    
</RollingFile>

The interval unit is determined by the smallest unit supplied in the file pattern date. In our case the smallest unit is the hour. If interval was specified to 2 it would roll over every two hours. Note the date lookup can be anywhere in the file pattern but must be present to do time based rollovers.