5

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>
Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40

5 Answers5

4

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>
HungNM2
  • 3,101
  • 1
  • 30
  • 21
1

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!

BretC
  • 4,141
  • 13
  • 22
1

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

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>
  • And where do I specify the interval, in my case 20 minutes ?? – Xavier DSouza Apr 16 '15 at 11:52
  • Ok..just 1 more doubt I have .. within which already defines the fileName and filePattern & so would this be overriden – Xavier DSouza Apr 16 '15 at 11:59
  • Do you have a working xml config for this? Or did it work for you?? – Xavier DSouza Apr 16 '15 at 12:01
  • I am sorry that config doesn't work for log4j2. trying to find something similar for the log4j version you use. What I found so far, is that you can combine triggering policies. the unit of interval attribute is "day" not minutes, the max attribute is the number of rollover files that you can keep at the same time. It seems that using minutes as a unit of your interval is not directly possible with log4j. I think you have to create a custom tirggeringPolicy. this question may be helpful for you : http://stackoverflow.com/questions/26539505/log4j2-registering-custom-triggeringpolicy – Mostafa EL BARRAK Apr 16 '15 at 12:19
  • Thanks.. I will try with the plugin in github - https://github.com/mushkevych/log4j2plugin/ hope it works..!! – Xavier DSouza Apr 16 '15 at 12:50
  • It's the same class I told you about in my last comment. I hope it works. good luck :). – Mostafa EL BARRAK Apr 16 '15 at 12:55
0

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.

Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
  • And also I had to package the plugin as a jar. So as always stackoverflow at rescue - http://stackoverflow.com/questions/29745938/log4j2-2-1-custom-plugin-not-detected-by-packages-attribute – Xavier DSouza Apr 30 '15 at 09:53