log4j2 Default rollover strategy not deleting logs - log4j2

I have log4j2 with a default rollover strategy set up like this -
<RollingFile name="RollingFile" fileName="cc" filePattern="logs/${baseFileName}-%d{yyyy-MM-dd}.log.gz">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="logs/">
<IfFileName glob="logs/${baseFileName}-*.log" />
<IfLastModified age="2d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
so it should be deleting the oldest files when it gets to over 2 days old correct?
my log files are stored in the base path of the project in a folder called logs..
however I just did a test run and it got to 5 files before I stopped it....
any idea what could be causing this?

Please check the below configuration for deleting old files:
DefaultRolloverStrategy max="10" means daily 10 files can be created max.
You can use IfAccumulatedFileCount exceeds="2" to control how many files will be present at all time.
will specify the age of the file, the files older than 2 days from present day will be deleted if the total number of files are grater than 2 (as specified in IfAccumulatedFileCount ).
<DefaultRolloverStrategy max="100">
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/app-*.log">
<IfLastModified age="2d">
<IfAny>
<IfAccumulatedFileCount exceeds="2" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
Maybe it is searching the file to delete in the incorrect path or your log files are not 2 day old.
which version of log4j you are using? it will work for Log4j-2.5 and above

Missing asterisk in the end. Please check -
<IfFileName glob="logs/${baseFileName}-*.log*" />

Related

Log4j2 rolling over file with wrong timestamp

I am using log4j2 -
org.apache.logging.log4j-log4j-api-2.14.1.jar
and for clean up policy, I am using CronbasedTriggerPolicy, after adding cron based trigger policy, whenever log4j2 rolls over the files, it replaces the date and time stamp with 0's
This is how my config looks like
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile fileName="data/appname-${date:yyyy-MM-dd_HH:mm:ss:SSS}.gz" filePattern="data/appname-%d{yyyy-MM-dd_HH:mm:ss.SSS}.%03i.gz" name="AppAppender">
<PatternLayout>
<pattern>%-6p %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %m%n
</pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10MB"/>
<CronTriggeringPolicy schedule="*/5 * * * *"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="data/" maxDepth="2">
<IfFileName glob="*appname-*.gz"/>
<IfLastModified age="1d"/>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger additivity="false" name="com.app.Test">
<AppenderRef ref="AppAppender"/>
</Logger>
</Loggers>
</Configuration>
When logs are being written to file file looks like this -
appname-2021-10-28_16:35:01:393.gz
After rolling over file changes to this-
appname-2021-10-28_00:00:00.000.001.gz
Why is log4j2 changing the timestamp to all 0s? and this started happening after I added the cron trigger policy, if I remove it, things work as expected. My final agenda is to have log4j2 delete all files that are older than 2 weeks. I am just playing with the config for a day right now.
Although, I am not still not clear on why cron trigger would change the timestamp to all 0s, I achieved what I wanted to achieve, my understanding of delete tag was not clear enough. I read through the log4j2 documentation and I realized I missed the part where they explained that delete works whenever roll over happens, for my needs I realized there was no need to have cron trigger policy in first place. The delete would have tried to run every time log4j2 rolls over the file.

log4j2 TimeBasedTriggeringPolicy not working as expected

I have configured the log4j2.xml file in such a way that application.log file will be created and it should be rollover daily.
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- Log4j2 Configuration -->
<!-- ===================================================================== -->
<Configuration status= "INFO">
<!-- Common properties used in all appenders -->
<Properties>
<Property name="logBaseDirectory">/apps/wsserver/8.5/bpm/logs/log4j/sbl</Property>
<Property name="logPattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1} %X{transaction.id} %m%n</Property>
<Property name="maxFileAge">90d</Property>
</Properties>
<!-- Define required appenders -->
<Appenders>
<!-- ============================================================================================================================================ -->
<!-- log4j 1.x to 2.x migratoin steps -->
<!-- DailyRollingFileAppender rolling file appender is no longer availble in log4j2. So we need to use RollingFile with TimeBasedTriggeringPolicy -->
<!-- Right most value in filePattern will be considered as Interval for TimeBasedTriggeringPolicy rolling -->
<!-- Orignal log file will be application.log and archive will be application-20200917.log.zip - Auto compression of file -->
<!-- Remove the all archived logs which are older than 90 days -->
<!-- ============================================================================================================================================ -->
<RollingFile name="application" fileName="${logBaseDirectory}/application.log" filePattern="${logBaseDirectory}/application-%d{yyyy-MM-dd}.log.zip">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy />
<CronTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logBaseDirectory}" maxDepth="1">
<IfFileName glob="application-*.log.zip" />
<IfLastModified age="${maxFileAge}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<!-- Define the list of loggers required -->
<Loggers>
<logger name="MediationServices" level="TRACE" additivity="false">
<appender-ref ref="application" />
</logger>
<root level="TRACE" additivity="false">
<appender-ref ref="application" />
</root>
</Loggers>
</Configuration>
But in JVM, applicatoin.log file is getting rollover after 10MB and if three times it is rolled, the first file is getting overwritten. That means at any point of time i am having application.log and application-2020-10-16.log.zip.
Why log4j2 (v2.13) is rolling over the files for every 10MB even though configured as daily? Any pointers identifying issue in log4j2 configuration is much apricated.
Issue has been identified. AS <SizeBasedTriggeringPolicy /> is defined in configuration file, log4j2 is considering 10MB default value as file size and getting rollover. After removing this tag, issue has been resolved.

Log4j2 stops logging to log file after clearing log file

I am running my application on a WebLogic within a Linux environment. after clearing the content of the log file using ">log4j2.log". my application stops logging at all.
I have tried restarting my application, and then the server but still no logs.
also tried deleting the file using rm -f and then doing the restart but still no logs. see the config below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="60">
<Appenders>
<RollingFile name="RollingFileAppender"
fileName="./PortalLogs/log4j2.log"
filePattern="./PortalLogs/log4j2.log.%i.%d{yyyy-MM-dd}"
immediateFlush="true">
<PatternLayout>
<Pattern>%d %-5p [%X{messageExtTxt}] [%c] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<RegexFilter regex="*DefaultMessageListenerContainer*" onMatch="DENY" onMismatch="ACCEPT"/>
<DefaultRolloverStrategy max="30"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.jms.listener.DefaultMessageListenerContainer" level="ERROR" >
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="RollingFileAppender" level="INFO"/>
</Root>
</Loggers>
</Configuration>
When I tried using delete from WinSCP instead and then restarted the server, the log files were again produced and with logs in it.
My question is that is there a way to clean the log files safely without causing this issue? thanks in advance!
EDIT: Linux's RM on the log file before restarting the server actually works and recreates the log file, but still is there a way to make it work using ">log4j2.log" or after modifying the file? we need this as we have a protocol to do an initial run for automated tests, clear everything and then do an actual run.

DefaultRolloverStrategy In log4j2

I am having trouble configuring the "DefaultRolloverStrategy" for log4j2.xml to do the following :-
Ensure ONLY the last 4 log files are kept and older ones should get deleted.
So just be clear, the last 4 log files could be over a number days or on the same day, therefore,
the last 4 log files could be with the same date or span over different dates.
Below is the contents of log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Appenders>
<!-- Console Appender -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- Rolling File Appender -->
<RollingFile name="File" fileName="app_log.log"
filePattern="app_log-%d{yyyy-MM-dd}.%i.log">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="2 KB" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="" maxDepth="1">
<IfFileName glob="app_log*.txt">
<IfAny>
<IfAccumulatedFileSize exceeds="5 KB" />
<IfAccumulatedFileCount exceeds="4" />
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.app.utilities" level="info" additivity="true">
<AppenderRef ref="File" />
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I run up my application as shown below
java -Dlog4j.configurationFile=./app-log4j2.xml -jar application.jar
The log is generated in the same directory from where the above command is invoked from.
Below is a sample history of log files :-
File Name Date Modified
app_log.log 8/27/2018 2:25 PM
app_log-2018-08-27.2.log 8/27/2018 2:25 PM
app_log-2018-08-27.1.log 8/27/2018 2:11 PM
app_log-2018-08-26.5.log 8/26/2018 2:01 PM
app_log-2018-08-26.4.log 8/26/2018 2:00 PM
app_log-2018-08-26.3.log 8/26/2018 1:58 PM
app_log-2018-08-26.2.log 8/26/2018 1:57 PM
app_log-2018-08-26.1.log 8/26/2018 1:56 PM
It seems the "DefaultRolloverStrategy" is having no effect.
I presume my configuration is wrong. However, I would very much appreciate for suggestions
to correct this please.
Also, if the requirenment was to change such that log files greater 20 days should be deleted.
How could that be acheived.
Thank you very much in advance for you help
Pete
Take a look at the following line:
<IfFileName glob="app_log*.txt">
But your log files don't end with .txt! See:
File Name Date Modified
app_log.log 8/27/2018 2:25 PM
You likely need to change it to:
<IfFileName glob="app_log*.log">
That's what really jumps out to me. There might be another few tweaks you have to make, but try that first.

how to roll out file every hour with max given files in log4j2

I want to roll out file on hourly basis and want to keep max 3 file , below are the configuration . But its not executing properly. can anyone help .
<RollingFile name="LogFile" fileName="logs/server.log" immediateFlush="false" append="true"
filePattern="logs/server-%d{yyyy-MM-dd-HH}.log">
<PatternLayout>
<Pattern><%d{MMM d, yyyy hh:mm:ss a}> <%5p> - %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<!-- <SizeBasedTriggeringPolicy size="5 KB" /> -->
</Policies>
<DefaultRolloverStrategy max="3" />
</RollingFile>
You did not mention, what exactly is not working. According to you configuration I guess, that you always have a single rolled file. It happens because you did not define a file patter with an index. Add an index into the file pattern:
<RollingFile name="LogFile" fileName="logs/server.log" immediateFlush="false" append="true"
filePattern="logs/server-%d{yyyy-MM-dd-HH}-%i.log">
...
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="3"/>
</RollingFile>
Up to three rolled files will be held; files with a higher index will be newer than files with a smaller index.

Resources