简体   繁体   English

达到阈值大小后,删除 log4j2 中的滚动日志文件

[英]Delete rolloed over log files in log4j2 after a threshold size is reached

I have migrated from logback to log4j2 since the syslog appender in logback did not support the RFC5424 format.我已经从 logback 迁移到 log4j2,因为 logback 中的 syslog appender 不支持 RFC5424 格式。

But now I want to roll over the log files up to a certain threshold and delete the older files.但是现在我想将日志文件滚动到某个阈值并删除旧文件。

This option is available in logback as "totalSizeCap".此选项在 logback 中作为“totalSizeCap”可用。 Below is the snippet from the logback.xml下面是 logback.xml 的片段

    rollingPolicy
        class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- roll over daily and when the file reaches 100 MB, max of 7 days or 3GB threshold -->
        <fileNamePattern>/data/storage/log/cms.%d{yyyy-MM-dd}.%i.Logs.gz
        </fileNamePattern>
        <maxFileSize>100MB</maxFileSize>
        <maxHistory>7</maxHistory>
        <totalSizeCap>3GB</totalSizeCap>
    </rollingPolicy>

Here the files are rolled over once their size reaches 100 MB and zipped.一旦文件大小达到 100 MB 并压缩,这里的文件就会滚动。 Also, max history of 7 days is maintained and older logs are deleted.此外,最多保留 7 天的历史记录,并删除较旧的日志。 But if the size of these files exceeds 3GB with in the 7days period, even then the old logs are removed.但是,如果这些文件的大小在 7 天内超过 3GB,那么旧日志也会被删除。

I did not find the equivalent configuration in log4j2.xml我在log4j2.xml中没有找到对应的配置

below is my log4j2.xml which does not work下面是我的 log4j2.xml,它不起作用

<RollingFile name="RollingFile"
                 fileName="/data/storage/log/cms.log"
                 filePattern="/data/storage/log/cms.%d{yyyy-MM-dd}.%i.Logs.gz">
        <PatternLayout pattern="${LOG_PATTERN}" />
        <Policies>
            <SizeBasedTriggeringPolicy size="100 MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
        <DefaultRolloverStrategy max ="100" totalSizeCap="3GB">
            <Delete basePath="/data/storage/log/" maxDepth="2">
                <IfFileName glob="*/cms*.Logs.gz" />
                <IfLastModified age="P7D" />
            </Delete>
        </DefaultRolloverStrategy>
    </RollingFile>
    <Syslog name="Syslog" format="RFC5424" host="127.0.0.1" port="514"
            protocol="UDP" appName="CMS" enterpriseNumber="25979" facility="LOCAL0" />

please help.请帮忙。

I'm afraid you can't do the deletion of old log files based on time for version 2.19.0 .恐怕您无法根据版本2.19.0的时间删除旧日志文件。 Instead, you can get your old files deleted by using size-based rollover strategies.相反,您可以使用基于大小的翻转策略删除旧文件。

In versions 2.5 and above, you can delete log files based on time. 2.5及以上版本支持按时间删除日志文件。 If you want to use version 2.5 and above, the answer here will be useful to you.如果你想使用2.5及以上版本,这里的答案将对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM