Time based triggering policy in log4j2
Asked Answered
I

8

33

I am trying to create new log files on an hourly basis. I am using TimeBasedTriggerringPolicy of lo4j2 in RollingFileAppender. Below is the sample xml configuration code i have taken from log4j2 official site.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingFile name        ="RollingFile"
                     fileName    ="logs/app.log"
                     filePattern ="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd-HH}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>

            <!-- *** -->
            <TimeBasedTriggeringPolicy interval="1"
                                       modulate="true"/>
            <!-- *** -->
            
            <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="error">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

In the interval attribute I have set 1 which signifies 1 hour. But still my file does not roll every 1 hour.

Please help me find any mistake.

Note : I have included beta9 of log4j2 (which is the latest)

Idle answered 10/10, 2013 at 19:5 Comment(0)
P
38

1 here indicates 1 day and not 1 hour. I have manually tested with below configuration.

<RollingFile name="T" fileName="/data_test/log/abc.log"
        filePattern="/data_test/log/abc-%d{MM-dd-yyyy}-%i.log">
        <PatternLayout>
            <Pattern>%d{ISO8601} %-5p [%t] (%F:%L) - %m%n</Pattern>
        </PatternLayout>
        <Policies>              
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="100 KB" />
        </Policies>
    </RollingFile>

For manual testing, I change the system date and time. First, try with increasing 1 hour. The log files will be generated but not as per the expectation. Then change the system date, increase by 1 day and then see the results.

Suppose the last log file (abc.log) on day 29-Oct is of 50 KB. Configuration size is 100 KB. If we change the day (increase by 1 day) and then run. Then, last file will be renamed 29-Oct-(some sequence number).log (50 KB file as it is copied) and new file will be created with abc.log

I have tried this with simple servlet with below configuration in web.xml

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>log4j2.xml</param-value>
</context-param>

keep log4j2.xml in src folder. log4j2.xml is not loaded if we keep it in classpath.

Peipus answered 30/10, 2013 at 10:22 Comment(4)
interval = How often a rollover should occur based on the most specific time unit in the date pattern. For example, with a date pattern with hours as the most specific item and and increment of 4 rollovers would occur every 4 hours. The default value is 1. So if you have a pattern like "yyyy-MM-dd-HH", file would be rolled every hour, and if it is "yyyy-MM-dd", file would be rolled every day.Adnate
and can I ask what what is replaced by %i in file patternHyderabad
I believe the %i is going to increment if you roll over based on the log file getting larger than the size limit, not time.Selestina
if we need to rollover the active file (for Ex: logs/app.log) based on a particular time, then what config need to be changed ?.Carliecarlile
C
12

Log4j documentations:

interval -> (integer) How often a rollover should occur based on the most specific time unit in the date pattern. For example, with a date pattern with hours as the most specific item and and increment of 4 rollovers would occur every 4 hours. The default value is 1.

You should change the filename pattern if you would like to create it every hour.

Circumferential answered 22/5, 2018 at 10:18 Comment(1)
if we need to rollover the active file (for Ex: logs/app.log) based on a particular time, then what config need to be changed ?.Carliecarlile
B
11

Everyday day Rolling

<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd}.log" ...>
   ...
<TimeBasedTriggeringPolicy interval="1" modulate="true" />

Everyday Hour Rolling

<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd-HH}.log" ...>
   ...
<TimeBasedTriggeringPolicy interval="1" modulate="true" />

Everyday 5 day Rolling

<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd}.log" ...>
   ...
<TimeBasedTriggeringPolicy interval="5" modulate="true" />

Everyday 5 hours Rolling

<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM-dd-HH}.log" ...>
   ...
<TimeBasedTriggeringPolicy interval="5" modulate="true" />

Every Month Rolling

<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app.%d{yyyy-MM}.log" ...>
   ...
<TimeBasedTriggeringPolicy interval="1" modulate="true" />

Hope these cases will helps very well in understanding how filePattern and interval are related.

Bronco answered 31/8, 2022 at 3:40 Comment(0)
U
6

As Abid mentioned, interval value is interpreted in context of pattern that is specified as part of filePattern. It starts with lowest denomination. For example,if pattern contains S, frequency will be in millisecond. It supports the date pattern as described in detail as part of SimpleDateFormat java doc http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Ultann answered 1/10, 2014 at 20:24 Comment(1)
Are you saying that id using appender.rolling.filePattern = %d{yyyy-MM-dd-HH-mm-ss}-%i.log.gz with appender.rolling.policies.time.interval = 1 it will roll every second?Brennen
J
5

You do have a non-empty log file (otherwise there is nothing to roll over)?

Note that even though the name is "TimeBased..." It will not actually roll over at the specified time, but at the first log event that arrives after the time threshold has been exceeded. Can you try with a small test program that logs something after 61 minutes or so and see if the problem still occurs?

If it doesn't roll over with the above test program, you may have found a bug. In that case please raise it on the log4j issue tracker. (Be sure to attach the test program the team can use to reproduce the issue).

Jeanene answered 10/10, 2013 at 22:56 Comment(0)
S
2

The time interval interpretation depends on file pattern you are using. The following configuration rolls file every second for me.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
            <Console name="A" target="SYSTEM_OUT">
                    <PatternLayout pattern="%d [%t] %-5p {%F:%L} %x - %m%n" />
                    <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            </Console>

            <RollingFile name="R"
                    fileName="/home/xxx/yyy/myapp.log" filePattern="/home/xxx/yyy/myapp-%d{yyyy-MM-dd-HH-mm-ss}-%i.log">
                    <PatternLayout pattern="%d [%t] %-5p {%F:%L} %x - %m%n" />
                    <Policies>
                            <TimeBasedTriggeringPolicy interval="1" />
                    </Policies>
            </RollingFile>
    </Appenders>


    <Loggers>
            <Root level="INFO">
                    <AppenderRef ref="A" />
                    <AppenderRef ref="R" />
            </Root>
    </Loggers>
</Configuration>
Seamount answered 7/6, 2021 at 7:39 Comment(0)
D
0

According to your TimeBasedTriggeringPolicy configuration, logger will only populate the logs every day not every hour. AFAIK,You can achieve the functionality by changing the filePattern from HH(Hours) to dd(Days).

I have modified your config.xml. Try This

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
   <Appenders>
      <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/$${date:yyyy-MM}/app-%d{yyyy-MM-dd}-%i.log.gz">
         <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
         </PatternLayout>
         <Policies>
            **
            <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            **
            <SizeBasedTriggeringPolicy size="250 MB" />
         </Policies>
      </RollingFile>
   </Appenders>
   <Loggers>
      <Root level="error">
         <AppenderRef ref="RollingFile" />
      </Root>
   </Loggers>
</Configuration>

For more details check this

Hope this will workout for you too.

Drumfire answered 14/2, 2019 at 5:15 Comment(0)
W
0

It depends on the file name you give

logs/$${date:yyyy-MM}/rollingfile-appender-%d{yyyy-MM-dd-HH-mm}.log

Above would generate every minute, similarly you can change the file patter. and appender.rolling.policies.time.interval = 1 could give the interval you need for

From you code above I see you have %i for index generation. Instead, you should remove the index (%i) from the filePattern since you're rolling files based on time intervals, not size-based triggers.

Wichman answered 17/2, 2024 at 20:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.