Log4j2 RollingFile appender sometimes not rolling over after midnight - log4j2

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.

Related

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.

Lo4j config - Log to different files based on type of mesasge prefix

Hello I am using Log4j2 for my logging and I am little confused as to how I can log message to a different file based on the message prefix.
For example, currently all messages are logged in a single logs folder.
A set of my messages look like this:
'com.project.latency: ProjectName=[MooPointProject].......'
Some of my other log messages are of the format:
'com.project.latency: ProjectName=[DataPlaneProject].......'
I want to log the messages which contain the MooPointProject in a specific file and one containing DataPlaneProject in a separate log file.
Is there a specific way I can do that other than changing the logging level itself?
Set up several appenders one for each project name. For example:
<RollingFile name="RollingFile" fileName="logs/MooPointProject.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*MooPointProject.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
and
<RollingFile name="RollingFile" fileName="logs/DataPlaneProject.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*DataPlaneProject.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
Then assign both the appenders to your logger(s). Since your RegexpFilter accepts only a project-spcific lines you will get them to the appropriate files separately.
P.S. - Find the detail on other types of filters here.

Log4j2 time based rolling

The following is my time and size based file rolling appender
<RollingFile name="fileWriter" fileName="${LOG_DIR}/file.log"
filePattern="${ARCHIVE}/file_log.%d{yyyy-MM-dd}-%i.gz">
<PatternLayout pattern="${PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
</RollingFile>
To reduce the number of log files, I would like to make it rolling over by time, so that the files generated 30 days ago is automatically removed.
Could DefaultRolloverStrategy help in my case? If it doesn't, would anyone provide some suggestions? Many Thanks.
Yes, the DefaultRolloverStrategy will do what you want. You should be able to configure:
<DefaultRolloverStrategy max="30"/>
As an alternative, you can also specify a Delete action something like:
<DefaultRolloverStrategy>
<Delete basePath="${ARCHIVE}" maxDepth="2">
<IfFileName glob="*/file_log-*.gz" />
<IfLastModified age="30d" />
</Delete>
</DefaultRolloverStrategy>

How to generate half-daily logs using log4j2?

I want to generate half-daily log files through log4j2 configuration. I have given :
<RollingFile name="fileAppender" filename ="${logName} append="true"
FilePattern="${logName}. %d{yyyy-MM-dd-a}">
And also defined:
<Policies>
<TimeBasedTriggeringPolicy interval="1"
Module="true"/>
But i am getting only AM log in this way though i want it to be something :
a.log.2016-03-23-AM
b.log.2016-03-23-PM
Can somebody help me out in the same?
You can use the CronTriggeringPolicy for this. I borrowed the expression from the answer at Cron Expression (Quartz) for a program to run every midnight at 12 am for the actual expression to use.
<RollingFile name="fileAppender" filename ="${logName} append="true" FilePattern="${logName}.$$d{yyyy-MM-dd-a}">
<CronTriggeringPolicy schedule="0 0 0,12 * * ?" />
</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"

Resources