log4j2 RollingFile only keeping 4 days - java

I tried setting up log4j to compress my log files and send them to a mount. I added a day/month/year to it in an attempt to get it in directories like that.
At first this appeared to be working.
Coming back a while later, it turns out only the most recent 4 days are present.
This is my config:
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p (%t) %c{1.} %m%n
</Pattern>
</PatternLayout>
</Console>
<RollingFile
name="RollingFile"
fileName="$/home/myservice/myservice.log"
filePattern="/mnt/logbackups/$${date:dd-MM-yyyy}/myservice-%d{dd-MM-yyyy}-%i.log.gz" >
<PatternLayout>
<Pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p (%t) %c{1.} %m%n
</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="32 MB"/>
</Policies>
</RollingFile>
</Configuration>
I tried looking for a cause in the documentation: https://logging.apache.org/log4j/2.x/manual/appenders.html
But I can't find anything. Then again it's a gigantic page, maybe I'm missing something.
Does anybody know why this is happening?

You defined the RollingFileAppender which will regularly write to a new logfile. You also defined the triggers when to write to a new file, but you did not set the Rollover Strategies.
You could use the DirectWrite Rollover Strategy with maxFiles not set (to a value greater than zero).
You could also try to configure the Log Archive Retention Policy/Delete on Rollover action. Set testMode=true to prevent any file from being deleted.
Examples are available on the huge page you mentioned.

What you looking for is the "Default Rollover Strategy" specified under Rollover Policies. You can add one explicitly to adjust the default parameters.
Specify it like this inside the RollingFile:
<DefaultRolloverStrategy max="20"/>
Although I am confused because according to their documentation the default is 7:
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

Related

Generate Logs in a new directory daily according to each hour in log4j2?

I want to create a directory {date}.dir and inside this directory, I need logs for each hour in a new separate file as 0000.log, 0100.log ...2300.log.
I used rolling file appender, but it only work to create a single log file and compressed the last one. I'm using time-based triggering policy with interval = 1, which gives me a new file every day and compress the last day's.
<RollingFile name="eventLogger" fileName="/Log/webservices/linpub.log" filePattern="/Log/webservices/linpub-%d{MM-dd-yyyy}.log.gz">
<PatternLayout>
<Pattern>%d{MM/dd/yy HH:mm:ss.SSS} %-5p [%t] %c{1} %X{trioOperation} - %m %throwable %n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
I want to create a new file with name as linpub-{date}.dir and inside the file, there should be a log file for each hour.
I'm using log4j2 2.3 version.
In case of <TimeBasedTriggeringPolicy interval="1" modulate="true"/>, here interval="1" means 1 day not 1 hour. If you want to achieve 1 hour basis, you have to use CronTrigger base policy. Find below the code.
<CronTriggeringPolicy schedule="0 0 0/1 1/1 * ? *" evaluateOnStartup="true"/>
Replace the following line with the above one.
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
Also change the file name patten with this yyyy-MM-dd-HH so that you will be able to know for hourly basic. Please check and test it.
Please use below configuration:
<appender name="dailyFileAppender"
class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true" />
<param name="Threshold" value="INFO" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="/temp/logs/Project-Name_%d{yyyy-MM-
dd-HH}.log" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p
%-10t [%-40.40c] %x - %m%n" />
</layout>
</appender>

log4j2 strange rolling log behavior and rmiregistry outputs "ERROR StatusLogger Log4j2 could not find a logging implementation..."

I just started using log4j2. I am using it for multiple JVMs that use RMI. I have two problems.
I start up the rmiregistry as it's own executable using the same CLASSPATH as the application. It starts without any errors, but once an RMI client talks to it, rmiregistry outputs this error: ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath... But the log4j-core jar file is already in the CLASSPATH and if there was a problem, then why does the application not output this error as well?
I am using the RollingFile appender with OnStartupTriggeringPolicy and SizeBasedTriggeringPolicy. In theory at startup I should get logs like:
foo.log - active log
foo.log.1 - old log rolled if I already started it before
foo.log.2 - older log rolled if I already started it more than once
But instead when I start it the first time, it rolls a bunch of times, even though the size limit has not been reached and the active log is one that should have been rolled. In other words, I get this:
foo.log
foo.log.1
foo.log.2
foo.log.3 - active log
foo.log.4
Config file (log4j2.xml) contents:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<!-- ********** APPENDERS *********** -->
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d (%-4r) [%t] %-5p %c %x - %m%n"/>
</Console>
<RollingFile
name="FOO_OUT"
fileName="/foo/log/foo.log"
filePattern="/foo/log/foo.log.%i"
append="true">
<PatternLayout>
<Pattern>%d (%-4r) [%t] %-5p %c %x - %m%n</Pattern>
</PatternLayout>
<DefaultRolloverStrategy max="24" fileIndex="min"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<asyncLogger name="au.com" level="DEBUG">
<AppenderRef ref="FOO_OUT"/>
</asyncLogger>
<asyncRoot level="debug">
<AppenderRef ref="FOO_OUT"/>
</asyncRoot>
</Loggers>
</Configuration>
The CLASSPATH includes a lot of jars, but main ones related to lo4j2 are:
/foo/lib/slf4j/slf4j-api-1.7.21.jar
/foo/lib/slf4j/jcl-over-slf4j-1.7.21.jar
/foo/lib/slf4j/log4j-slf4j-impl-2.5.jar
/foo/lib/log4j2/log4j-core-2.5.jar
/foo/lib/log4j2/log4j-api-2.5.jar
/foo/lib/log4j2/disruptor-3.3.4.jar

log4j to log4j2 custom RollingPolicy

We have a custom rolling policy that is declared in log4j like this:
log4j.appender.testing.rollingPolicy=com.custom.appender.TimeBasedRollingPolicy
log4j.appender.testing.rollingPolicy.timeToRolloverInSeconds=60
log4j.appender.testing.rollingPolicy.FileNamePattern=/tmp/cdr.log
How can this be declared in log4j2.xml?
Log4j2 has a built-in time based rollover policy that may do what you want. The below configuration results in a rollover every minute:
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/old/app-%d{yyyyMMdd-HHmm}-log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
If you want to create a custom rollover policy, you would need to create a log4j2 plugin that implements TriggeringPolicy. A good starting point would be to look at the source code for the built-in TimeBasedTriggeringPolicy. General information on Log4j custom plugins is here.

DailyRollingFileAppender alternative in Log4j 2x

DailyRollingFileAppender was removed in Log4j 2x. What should be used instead?
Use a RollingFile 1 with TimeBasedTriggeringPolicy 2. e.g.:
<RollingFile name="RollingFile"
fileName="/tmp/app.log"
filePattern="/tmp/app%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
<TimeBasedTriggeringPolicy />
</RollingFile>
See more in Apache Log4j 2 User's Guide [PDF].
Notes
org.apache.logging.log4j.core.appender.RollingFileAppender
org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy
If, like me, you're finally getting around to upgrading to log4j2 – because of the log4shell vulnerability – and you are accustomed to DailyRollingFileAppender rolling over at midnight, you should make that:
<TimeBasedTriggeringPolicy modulate="true" />

How to add/subtract date in log4j 2?

i have following configuration file in log4j 2
<RollingFile name="RollingFile" fileName="logs/test.log"
filePattern="logs$${date:yyyyMM}/app-%d{MM-dd-yyyy}.log">
<PatternLayout pattern="%d %-5p [%t] %C{4} (%F:%L) - %m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="100"/>
</RollingFile>
can I perform date addition and/or subtraction in the filePattern. ? as of now the files generated upon rollover has current date. I would like to subtract single day from it . how can it be done ?
This is currently not possible, but I believe someone requested something similar earlier. I believe filing your request on the Log4j-2.0 issue tracker ( https://issues.apache.org/jira/browse/LOG4J2 ) may be the best way to proceed.

Categories