Log4j2 RollingFile with daily rotation - log4j2

Using Log4j2 2.8, is there a way to have my logs rotate daily but so that the current file has a constant name?
Example:
my.log <-- current day (2017-02-13)
my-2017-02-12.log <-- last log
my-2017-02-11.log <-- second to last log
I tried the following configuration without success:
<RollingFile name="RollingFileMain" fileName="my.log"
filePattern="my-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%level{length=1} %d{yyyy-MM-dd HH:mm:ss} %c{-3} (%F:%L) %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="2"/>
</RollingFile>
Additionally, I get the following exception when I launch my application for the first time on a given day:
2017-02-14 09:28:38,334 main ERROR An exception occurred processing Appender RollingFileMain java.lang.NullPointerException
at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:107)
at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:96)
at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:89)
at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.purgeAscending(DefaultRolloverStrategy.java:212)
at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.purge(DefaultRolloverStrategy.java:199)
at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.rollover(DefaultRolloverStrategy.java:326)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.rollover(RollingFileManager.java:310)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.rollover(RollingFileManager.java:227)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.checkRollover(RollingFileManager.java:207)
at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:267)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:433)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:403)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2091)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1988)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1960)
at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1297)
at my.package.MainKt.main(Main.kt:48)

Please change your filepattern to filePattern="my-%d{yyyy-MM-dd}.log":
The %i is only useful when you use the SizeBasedTriggeringPolicy. If you only have a TimeBasedTriggeringPolicy, Log4j gets confused by the %i...

The issue seems to be a combination of #Remko Popma's answer and a bug in Log4j2 2.8. After applying the filePattern="my-%d{yyyy-MM-dd}.log" and downgrading to version 2.7, everything works as expected.
I submitted an issue regarding the crash here: https://issues.apache.org/jira/browse/LOG4J2-1815

Related

Using the start timestamp in a RollingFile

I have the following RollingFileAppender setup:
<RollingFile name="RollingFile">
<FileName>${LOG_DIR}system.log</FileName>
<FilePattern>${LOG_DIR}system%d{yyyy-MM-dd HH.mm.ss}.log.zip</FilePattern>
<JsonLayout compact="true" eventEol="true" />
<Policies>
<OnStartupTriggeringPolicy />
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
</Policies>
<!-- roll over strategy, with unlimited files created -->
<DefaultRolloverStrategy fileIndex="nomax" />
</RollingFile>
It does nearly exactly what i want (new log file at midnight or on startup/restart), but unfortunately the timestamps that get assigned to the rolled over log files are sometimes simply incorrect. So for example:
system.log // current log, started at 2022-08-10 at midnight
system2022-08-09 00.00.00.log.zip // started at 2022-08-09 at midnight, so this is fine
system2022-08-08 00.00.00.log.zip // started at 2022-08-08 10.09 am, so this is not fine
system2022-08-08 10.04.57.log.zip // started at 2022-08-08 at 8.55 am, so this is not fine
As you can see system2022-08-08 00.00.00.log.zip is using a completely wrong timestamp and system2022-08-08 10.04.57.log.zip is using the timestamp when the log ended.
I'm not sure what i'm doing wrong here. Appreciate any suggestions on how to properly combine OnStartupTriggeringPolicy and CronTriggeringPolicy to achieve my goal here.
Seems like this behavior is unfortunately a bug in log4j2, and an old one. See my comment here: https://issues.apache.org/jira/browse/LOG4J2-2148
I also posted a solution for log4j2 2.18.0 in there, which will atleast does what i desire - there are still several things wrong in log4j2 itself though.

How to generate AM and PM log filename using log4j2.13 version

Helllo everyone,
we have log4j from 1.2.8 to log4j2.11, and observed half daily logs files are not generating as expected.Only i can see only AM log files are generating.
<RollingFile name="file" fileName="testAMPM.log" filePattern="testAMPM.log-%d{yyyy-MM-dd-a}" >
<PatternLayout pattern="%d %5p %-120m [%t - %c:%L] %n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
Will not mark this as duplicate because this, question here did not get answered. The answer to another duplicate question does have an example though, so please try this here first:
<RollingFile name="fileAppender" filename ="${logName} append="true" FilePattern="${logName}.$$d{yyyy-MM-dd-a}">
<CronTriggeringPolicy schedule="0 0 0,12 * * ?" />
</RollingFile>
A comment in the first linked duplicate also suggests to check out Cron Triggering Policy, which could provide a better solution to your requirement than a TimeBasedTriggeringPolicy:
Just try it out.

DefaultRolloverStrategy does not delete any file

I used the following Log4j2 configuration to test the behavior. It rotates the file every 10 seconds and should keep only 3 files. However no file is deleted. What am I missing?
Please don't suggest to use the delete action.
<RollingFile name="File" fileName="/var/log/mylog.log" filePattern="/var/log/mylog-%d{yyyy-MM-dd-HH-mm-ss}.log.gz">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="10"/>
</Policies>
<DefaultRolloverStrategy max="3"/>
</RollingFile>
I realise this is an old question, but I had the same problem.
I also realise the OP said "Please don't suggest to use the delete action" so this may mean there are version restrictions on using Delete.
However I found this feature request which sheds some light on this behaviour - a comment there indicates.
DefaultRolloverStrategy max attribute only applies if you have a %i in
the file pattern.
The manual, under "Default Rollover Strategy", indicates
The default rollover strategy accepts both a date/time pattern and an
integer from the filePattern attribute specified on the
RollingFileAppender itself
and
max : integer : "The maximum value of the counter. Once this values is
reached older archives will be deleted on subsequent rollovers. The
default value is 7."
A bit a mix of "counter" and "integer" - but think (now) that it means this only applies to "%i" pattern. If the manual used "integer counter" it may be clearer.
A solution I got working (using 2.10.0 and days rather than seconds) using Delete (sorry OP) was:
<RollingFile name="MyLog" fileName="${sys:dataDir}/mylog.log" append="true">
<FilePattern>./mylog-%d{yyyy-MM-dd}.log.zip</FilePattern>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level - %msg %logger{36}%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${sys:dataDir}" maxDepth="1">
<IfFileName glob="mylog-*.log.zip" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile >

log4j2 RolllingFileAppender filePattern create a wrong date

I used date in filePattern, but the date log4j create is not right.
Today is 2015-11-23, but log4j create a file directory named 2015-12.
my log4j2 version is 2.4.1.
here is a snapshot
below is my log4j2 configuration.
<RollingFile name="RollingFile" fileName="logs/executor.log" append="true"
filePattern="logs/$${date:yyyy-MM}/executor-%d{yyyy--MM--dd}-%i.log.gz">
<PatternLayout>
<Pattern>%t %d{yyyy-MM-dd HH:mm:ss} %c %p -%m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="24" modulate="true"/>
<SizeBasedTriggeringPolicy size="1MB"/>
</Policies>
</RollingFile>
This issue is due to your intervals. Its effectively setting up the initial rollover file to be when the end of the next rollover would be. The documentation on the RollingFileAppender for the TimeBasedTriggeringPolicy says: "How often a rollover should occur based on the most specific time unit in the date pattern." Since your most specific time unit is dd, or day of month, its setting it up for 24 days from 2015-11-23, or sometime in month 12, when it would rollover.
What you probably want is an interval of 1. This should rollover each file day and then once a month spills over, it would into the next subfolder because 1 day was triggered.
I've tested this with your pattern, but use a MM at the end and and a interval="24." Modulated, it rolled into 12/2016, but not modulated, it rolled over saying 11/2017! I can't say why and per my comment above, there have been issues with this feature reported.
Have same problem.
<RollingFile name="DailyFileAppender" fileName="logs/server.log"
filePattern="logs/server - %d{yyyy-MM-dd} - %i.log">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="5"/>
<SizeBasedTriggeringPolicy size="10 KB"/>
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>
The output file's data is wrong. example the date is "2015-12-01" the output date is "2015-12-05"

Log4j2 RollingFile appender sometimes not rolling over after midnight

I have a RollingFile appender
<RollingFile name="xxx" fileName="${sys:catalina.base}/logs/request.log" filePattern="${sys:catalina.base}/logs/request.log.%d{yyyy-MM-dd}.%i.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %msg%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<MarkerFilter marker="REQUEST_MARKER" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
Which works perfect but this weekend at 14.02 and 15.02. the logger did not log anything after midnight between 00:00:00 and 03:39:17 and at 15.02. between 00:00:00 and 03:18:59.
Can anyone imagine why?
Btw: the application was okay, as I received data in other logfiles.
Regards
S.
The title of your question is about not rolling over but your question makes it sound like you are asking why no logs were written during certain times, so I am not sure if I am answering your question.
The RollingFileAppender only checks for a rollover when it writes events. If no log event occurs between midnight and 3:18 am then the file will not roll over until 3:18 am.

Resources