Below is my log4j configuration file, which should be configured as follows:
- maximum 5 backups of 20 MB each
- backup at midnight regardless of the current size of the main file
- deleting logs older than 21 days
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">${sys:user.home}/test</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${basePath}/console.log" filePattern="${basePath}/console_backup/console-%d{yyyy-MM-dd}_%i.log">
<PatternLayout>
<pattern>%msg</pattern>
</PatternLayout>
<Policies>
<CronTriggeringPolicy schedule="00 00 00 * * ?"/>
<!-- MAKSYMALNY ROZMIAR PLIKU -->
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
<!-- MAKSYMALNA ILOSC BACKUPOW -->
<DefaultRolloverStrategy max="5">
<Delete basePath="${basePath}/console_backup" >
<IfLastModified age="21d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="name.john.doe.util.gui.ConsoleOutputStream" level="info" additivity="true">
<appender-ref ref="fileLogger" level="info" />
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
According to the assumption, I would like the log backup to contain the date of the log creation, i.e. the backup was created on July 8, 2024, it should contain the name “console-2024-07-08_1.log” but it contains the date of the previous day (“console -2024-07- 07_1.log”). After changing the logging policy and replacing CronTriggeringPolicy with the <TimeBasedTriggeringPolicy interval=”1” modulate=”true”/> entry, the backup file contains the correct date. Please help me find the cause. I would like to use “CronTriggeringPolicy” because it gives me more options.
The problem is that from time to time the logger stops working, i.e. a backup is created at midnight, the main log file is created but nothing is written to it, restarting the application solves the problem.
Nef91 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2