apache / logging-log4j2

Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
https://logging.apache.org/log4j/2.x/
Apache License 2.0
3.4k stars 1.62k forks source link

CronTrigerringPolicy date #2721

Open Nef91 opened 4 months ago

Nef91 commented 4 months ago

Hello, below is my login configuration file, the main assumptions are:

<?xml version="1.0" encoding="UTF-8"?>

${sys:user.home}/.test/logs %msg

According to the assumption, I would like the log backup to include the date of log creation, i.e. if I create a log on July 8, 2024, the log backup should contain the name 'console-2024-07-08_1.log' and it creates a backup with the date from the previous day (console -2024-07-07_1.log). After changing the logging policy and replacing CronTriggeringPolicy with the entry <TimeBasedTriggeringPolicy interval="1" modulate="true"/>, it logs the correct date, but I would like to have more control over it, so I want to use CronTriggeringPolicy. Please help me find what could be the cause.

ppkarwasz commented 3 months ago

Hi @Nef91,

Thank You for reaching out.

The %d pattern of the filePattern should be replaced with the timestamp of the previous rollover. For example at midnight of 2024-07-08 the rolling file appender will move console.log to console-2024-07-07_1.log. The reason for this behavior is that the console-2024-07-07_1.log file contains log events issued from 2024-07-07 00:00 to 2024-07-07 23:59.

The TimeBasedTriggeringPolicy should behave the same way CronTriggeringPolicy does and the fact that it uses the timestamp of the current rollover is IMHO a bug. @rgoers, what do you think?

In any case, you can use the $${date:yyyy-MM-dd} runtime lookup together with CronTriggeringPolicy, which will be expanded to the current date.