Log4j2 auto rollover after specified duration
Asked Answered
M

5

6

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>
Middy answered 16/4, 2015 at 10:49 Comment(0)
M
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.

Middy answered 30/4, 2015 at 9:43 Comment(1)
And also I had to package the plugin as a jar. So as always stackoverflow at rescue - #29746438Middy
I
5

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>
Illyes answered 8/7, 2021 at 4:6 Comment(0)
Q
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!

Quadri answered 16/4, 2015 at 11:36 Comment(0)
E
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.

Erdmann answered 28/8, 2017 at 16:54 Comment(0)
H
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>
Highup answered 16/4, 2015 at 11:50 Comment(6)
And where do I specify the interval, in my case 20 minutes ??Middy
Ok..just 1 more doubt I have <Policies>..</Policies> within <RollingFile> which already defines the fileName and filePattern & so would this be overridenMiddy
Do you have a working xml config for this? Or did it work for you??Middy
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 : #26540005Highup
Thanks.. I will try with the plugin in github - github.com/mushkevych/log4j2plugin hope it works..!!Middy
It's the same class I told you about in my last comment. I hope it works. good luck :).Highup
M
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.

Middy answered 30/4, 2015 at 9:43 Comment(1)
And also I had to package the plugin as a jar. So as always stackoverflow at rescue - #29746438Middy

© 2022 - 2024 — McMap. All rights reserved.