3
votes

I am using RollingFile appender. I want the log file to be rolled after every 20 minutes irrespective of the logging event. For instance in an hour I should have 3 log files even though there might have not been any logging in that hour. Is this possible using Log4j2? If yes please provide the configuration ( in log4j2.xml) that are required. The below config does not seem to work :

       <RollingFile name="RECHARGE_NMCD" fileName="D:/rc_nmcd/rc_nmcd.log" append="true" bufferedIO="false" filePattern="D:/rc_nmcd/rc_nmcd_%d{yyyy-MM-dd-HH-mm}.process">
            <PatternLayout>
                <Pattern>%m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="20"/>
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingFile>
5

5 Answers

1
votes

I don't think you can make Log4J2 roll every N minutes out of the box, it looks like you can get it to do this every minute, hour, day but not 20 minutes. (See https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html - you can change it to "every minute" using a different date pattern)

I've not tried this, but there might be a way of customising this by providing a custom Rollover Strategy...

https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.html

If this works, please post your answer for other people to learn from!

1
votes

We may can do this using Corn Expression policy

CronTriggeringPolicy schedule="0 0/20 * 1/1 * ? *"/>.

This will roll your file automatically every after 20 minutes, irrespective of logging event.

0
votes

try this :

<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>foo.%d{yyyyMMdd-HHmm}.gz</fileNamePattern>
      <cleanHistoryOnStart>true</cleanHistoryOnStart>
      <maxHistory>20</maxHistory>
</rollingPolicy>

instead of

<Policies>
   <TimeBasedTriggeringPolicy interval="20"/>
</Policies>
0
votes

I referred this plugin https://github.com/mushkevych/log4j2plugin

I had a Runnable thread per FTimeBasedTriggeringPolicy which would actually sleep upto next rollover instead of the LogRotateThread which sleeps for some indefinite specified time.

Thread rotateThread = new Thread(new LogRotateRunnable(this));
rotateThread.start();


Added the above after initialize(RollingFileManager)

LogRotateRunnable :

while (true) {
        long sleepTime = fTimeBasedTriggeringPolicy.getNextRollover()
                - System.currentTimeMillis();
        if (sleepTime > 0) {
            try {
                Thread.sleep(sleepTime + EMPTY_LOG_EVENT_DELAY);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        fTimeBasedTriggeringPolicy.checkRollover(new EmptyLogEvent());
    }


Also it won't roll empty files, but the good part is of course if atleast one valid log entry within next rollover time, it will.

0
votes

change the filePattern to %d{yyyy-MM-dd_HH-mm-ss} for second unit

%d{yyyy-MM-dd_HH-mm} is minute unit

%d{yyyy-MM-dd_HH} is hour unit

%d{yyyy-MM-dd} is day unit

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" >
    <Properties>
        <Property name="LOG_PATTERN_7">%d{yyyy/MM/dd HH:mm:ss.SSS} [%-6p] %c.%M(%F:%L) – %m%n</Property>
    </Properties>

    <Appenders>
        <RollingFile name="RollingFile" fileName="logs/app.log" 
            filePattern="logs/$${date:yyyy-MM-dd}/${env:APP_NAME:-app}-%d{yyyy-MM-dd_HH-mm}_%i.log.zip">
            <PatternLayout pattern="${LOG_PATTERN_7}" />
            <Policies>
                <!-- filePattern %d{yyyy-MM-dd_HH-mm-ss}: interval = 20 second -->
                <!-- filePattern %d{yyyy-MM-dd_HH-mm}: interval = 20 minutes -->
                <!-- filePattern %d{yyyy-MM-dd_HH}: interval = 20 hours -->
                <TimeBasedTriggeringPolicy interval="20" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy max="1000" />
        </RollingFile>

        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN_7}" />
        </Console>
    </Appenders>

    <Loggers>
        <Root level="all" includeLocation="true">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFile" />
        </Root>
    </Loggers>

</Configuration>