log4j2 CronTriggeringPolicy results incorrect date in filename along with SizeBasedTriggeringPolicy

17 Views Asked by At

The log4j2 libraries as below

<properties>
    <jdkVersion>1.8</jdkVersion>
    <log4jVersion>2.17.1</log4jVersion>
</properties>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
</dependency>

Given the following log4j2 configurations

<Properties>
    <Property name="baseDir">${env:APP_HOME}/logs/${web:contextPath}</Property>
    <Property name="filename">${baseDir}/DeviceManagement.log</Property>
    <Property name="filePattern">${baseDir}/DeviceManagement-%d{yyyy-MM-dd}-%i.log</Property>
    <Property name="layoutPattern">[%5p][%t][%c.%M():%L][%d{yy/MM/dd HH:mm:ss.SSS}][%4r]%n%m%n</Property>
</Properties>

<Appenders>
    <Appender type="Console" name="STDOUT">
        <Layout type="PatternLayout" pattern="${layoutPattern}"/>
    </Appender>
    <Appender type="RollingFile" name="ROLLING_FILE" fileName="${filename}" 
                filePattern="${filePattern}">
        <Layout type="PatternLayout" pattern="${layoutPattern}"/>
        <Policies>
            <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
            <SizeBasedTriggeringPolicy size="5 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="20">
            <Delete basePath="${baseDir}" maxDepth="2">
                <IfFileName glob="{DeviceLog/*.log,DeviceManagement-*.log}"/>
                <IfLastModified age="P60D">
                    <IfAny>
                        <IfAccumulatedFileSize exceeds="100 MB" />
                        <IfAccumulatedFileCount exceeds="60" />
                    </IfAny>
                </IfLastModified>
            </Delete>
        </DefaultRolloverStrategy>
    </Appender>
</Appenders>

Expected result: The log file should roll over at midnight every day, with the current date as the filename. Size-based rotation should occur up to 20 files per date.

What's wrong: Date rotation yields the previous day's date in the filename instead of the current date. And the size-based rotation stops to rotate when the index is up to 20.(As the file list below)

2024/03/19  AM 02:26         5,246,621 DeviceManagement-2024-03-18-1.log <----Rotated at 3/19 02:26 AM with the filename 2024-03-18 which shall be 2024-03-19.
2024/03/19  AM 02:54         5,242,886 DeviceManagement-2024-03-18-2.log
2024/03/19  AM 03:21         5,242,887 DeviceManagement-2024-03-18-3.log
2024/03/19  AM 03:50         5,248,627 DeviceManagement-2024-03-18-4.log
2024/03/19  AM 04:20         5,243,058 DeviceManagement-2024-03-18-5.log
2024/03/19  AM 04:50         5,243,757 DeviceManagement-2024-03-18-6.log
2024/03/19  AM 05:21         5,245,418 DeviceManagement-2024-03-18-7.log
2024/03/19  AM 05:52         5,247,493 DeviceManagement-2024-03-18-8.log
2024/03/19  AM 06:25         5,243,024 DeviceManagement-2024-03-18-9.log
2024/03/19  AM 06:56         5,243,052 DeviceManagement-2024-03-18-10.log
2024/03/19  AM 07:29         5,243,677 DeviceManagement-2024-03-18-11.log
2024/03/19  AM 08:02         5,245,473 DeviceManagement-2024-03-18-12.log
2024/03/19  AM 08:31         5,243,077 DeviceManagement-2024-03-18-13.log
2024/03/19  AM 08:54         5,243,030 DeviceManagement-2024-03-18-14.log
2024/03/19  AM 09:07         5,243,012 DeviceManagement-2024-03-18-15.log
2024/03/19  AM 09:20         5,243,273 DeviceManagement-2024-03-18-16.log
2024/03/19  AM 09:43         5,243,066 DeviceManagement-2024-03-18-17.log
2024/03/19  AM 10:06         5,249,359 DeviceManagement-2024-03-18-18.log
2024/03/19  AM 10:28         5,243,121 DeviceManagement-2024-03-18-19.log
2024/03/19  AM 10:47         5,242,932 DeviceManagement-2024-03-18-20.log <-----Wrote the file up to index 20 at 10:47 AM and stop to rotate to the index 1(DeviceManagement-2024-03-18-1.log)
2024/03/20  AM 02:11           151,699 DeviceManagement.log <-----Active file
0

There are 0 best solutions below