Skip to content

DeleteAction in RollingFileManager does not work with soft link path #1548

@umgsai

Description

@umgsai

Description

The path of log files is soft link, writing log files works good, but the DeleteAction in RollingFileManager does not work

Configuration

Version: [Log4j version]
2.17.2

Operating system: [OS and version]
Linux 10008-5ck7v 5.10.56-220320.el7.centos.x86_64 #1 SMP Sun Mar 20 08:36:08 EDT 2022 x86_64 x86_64 x86_64 GNU/Linux

JDK: [JDK distribution and version]
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (Temurin)(build 25.302-b08, mixed mode)

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" packages="com.xx.xx">
    <Properties>
        <!-- log_base_dir is soft link -->
        <Property name="log_base_dir">/opt/logs/10008</Property>
        <Property name="log_pattern"
                  value="[%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5p][%C{1}.%M:%L][%t][msgId:%X{msgId}]%msg%xEx%n"/>
        <Property name="max_single_file_size">200KB</Property>
    </Properties>
    <Appenders>

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${log_pattern}"/>
        </Console>

        <RollingFile name="RollingFileInfo" fileName="${log_base_dir}/info.log"
                     filePattern="${log_base_dir}/info-%d{yyyy-MM-dd-HH}-%i.log.gz">
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="ACCEPT"/>
            <PatternLayout pattern="${log_pattern}" charset="UTF-8"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="${max_single_file_size}"/>
            </Policies>
            <DefaultRolloverStrategy max="10">
                <!-- basePath is soft link -->
                <Delete basePath="/opt/logs/10008" maxDepth="1">
                    <IfFileName glob="info-*.log.gz">
                        <IfAny>
                            <IfLastModified age="1H"/>
                        </IfAny>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="INFO">
            <!--<AppenderRef ref="Console"/>-->
        </Root>

        <AsyncLogger name="com.xx" level="DEBUG" includeLocation="true" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFileInfo"/>
        </AsyncLogger>
    </Loggers>
</Configuration>

linux disk

[op@10008-rppm5 logs]$ 
[op@10008-rppm5 logs]$ pwd
/opt/logs
[op@10008-rppm5 logs]$ ll
lrwxrwxrwx 1 deploy deploy 28 Jul 11 21:04 10008 -> /mnt/sandbox/10008

when using the log4j2 config file above, the RollingFileManager logs as below, IfFileName REJECTED

2023-07-11 21:11:51,997 Log4j2-TF-2-RollingFileManager-4 DEBUG Starting DeleteAction[basePath=/opt/logs/10008, options=[], maxDepth=1, conditions=[IfFileName(glob:info-*.log.gz AND [IfAny[IfLastModified(age=PT1H)]])]]
2023-07-11 21:11:52,001 Log4j2-TF-2-RollingFileManager-4 DEBUG DeleteAction complete in 0.004209355 seconds
2023-07-11 21:11:52,001 Log4j2-TF-2-RollingFileManager-4 TRACE Sorted paths:
2023-07-11 21:11:52,002 Log4j2-TF-2-RollingFileManager-4 TRACE /opt/logs/10008 (modified: 2023-07-11T13:11:20.375534Z)
2023-07-11 21:11:52,002 Log4j2-TF-2-RollingFileManager-4 TRACE IfFileName REJECTED: 'glob:info-*.log.gz' does not match relative path ''
2023-07-11 21:11:52,002 Log4j2-TF-2-RollingFileManager-4 TRACE Not deleting base=/opt/logs/10008, relative=

then I update the config file as below

            <DefaultRolloverStrategy max="10">
                <!-- basePath is real disk path -->
                <Delete basePath="/mnt/sandbox/10008" maxDepth="1">
                    <IfFileName glob="info-*.log.gz">
                        <IfAny>
                            <IfLastModified age="1H"/>
                        </IfAny>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>

the RollingFileManager logs as below, IfFileName ACCEPTED

2762188:2023-07-11 21:20:19,150 Log4j2-TF-2-RollingFileManager-13 DEBUG Starting DeleteAction[basePath=/mnt/sandbox/10008, options=[], maxDepth=1, conditions=[IfFileName(glob:info-*.log.gz AND [IfAny[IfLastModified(age=PT1H)]])]]
2023-07-11 21:20:19,152 Log4j2-TF-2-RollingFileManager-13 DEBUG DeleteAction complete in 0.001599611 seconds
2023-07-11 21:20:19,152 Log4j2-TF-2-RollingFileManager-13 TRACE Sorted paths:
.........
2023-07-11 21:20:19,153 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-9.log.gz (modified: 2023-07-11T13:20:07.361429Z)
2023-07-11 21:20:19,153 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-8.log.gz (modified: 2023-07-11T13:20:07.217431Z)
2023-07-11 21:20:19,153 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-7.log.gz (modified: 2023-07-11T13:20:06.969435Z)
2023-07-11 21:20:19,153 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-6.log.gz (modified: 2023-07-11T13:20:06.796437Z)
2023-07-11 21:20:19,154 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-5.log.gz (modified: 2023-07-11T13:20:06.393443Z)
2023-07-11 21:20:19,154 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-4.log.gz (modified: 2023-07-11T13:20:06.030448Z)
2023-07-11 21:20:19,154 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-3.log.gz (modified: 2023-07-11T13:20:05.692453Z)
2023-07-11 21:20:19,154 Log4j2-TF-2-RollingFileManager-13 TRACE /mnt/sandbox/10008/info-2023-07-11-21-2.log.gz (modified: 2023-07-11T13:20:01.567513Z)

2023-07-11 21:20:19,161 Log4j2-TF-2-RollingFileManager-13 TRACE IfFileName ACCEPTED: 'glob:info-*.log.gz' matches relative path 'info-2023-07-11-21-10.log.gz'
2023-07-11 21:20:19,162 Log4j2-TF-2-RollingFileManager-13 TRACE IfLastModified REJECTED: info-2023-07-11-21-10.log.gz ageMillis '12' < 'PT1H'
2023-07-11 21:20:19,162 Log4j2-TF-2-RollingFileManager-13 TRACE Not deleting base=/mnt/sandbox/10008, relative=info-2023-07-11-21-10.log.gz
2023-07-11 21:20:19,163 Log4j2-TF-2-RollingFileManager-13 TRACE IfFileName REJECTED: 'glob:info-*.log.gz' does not match relative path 'catalina.2023-07-11.log'
2023-07-11 21:20:19,163 Log4j2-TF-2-RollingFileManager-13 TRACE Not deleting base=/mnt/sandbox/10008, relative=catalina.2023-07-11.log
2023-07-11 21:20:19,163 Log4j2-TF-2-RollingFileManager-13 TRACE IfFileName ACCEPTED: 'glob:info-*.log.gz' matches relative path 'info-2023-07-11-21-9.log.gz'
2023-07-11 21:20:19,163 Log4j2-TF-2-RollingFileManager-13 TRACE IfLastModified REJECTED: info-2023-07-11-21-9.log.gz ageMillis '11802' < 'PT1H'
2023-07-11 21:20:19,164 Log4j2-TF-2-RollingFileManager-13 TRACE Not deleting base=/mnt/sandbox/10008, relative=info-2023-07-11-21-9.log.gz
2023-07-11 21:20:19,164 Log4j2-TF-2-RollingFileManager-13 TRACE IfFileName ACCEPTED: 'glob:info-*.log.gz' matches relative path 'info-2023-07-11-21-8.log.gz'

How to make RollingFileManager works good when using soft link basePath?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions