SizeBasedTriggeringPolicy限制不起作用 [英] SizeBasedTriggeringPolicy limit is not working

查看:705
本文介绍了SizeBasedTriggeringPolicy限制不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的log4j2 xml中具有以下设置

I have the following setting in my log4j2 xml

<Appenders>
        <RollingFile fileName="C:\logs\Fatal.log" filePattern="C:\logs\Fatal-%i.log" name="FatalFile">
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <PatternLayout pattern="[%d{ISO8601}] %-5level %logger{6} - %msg%n"/>
        </RollingFile>
</Appenders>

当我的代码长时间运行时,会创建7个日志,但是此后,日志大小开始超过10MB限制,并且对所有7个文件来说都变得很大.

when my code runs for long duration, there are 7 logs getting created but, after that the log size starts to grow beyond the 10MB limit and becomes very large for all 7 files.

我需要知道为什么只创建7个文件,以及如何限制文件大小不超过10 MB.

I need to know why there is only 7 files created and how can limit the file size not to grow beyond 10 MB.

推荐答案

DefaultRolloverStrategy在同一天最多创建7个档案.文件大小超过10 MB可能是您使用的旧版本Log4j 2的错误.

The DefaultRolloverStrategy creates up to 7 archives on the same day. File size growing beyond 10 MB may be a bug in the old version of Log4j 2 you are using.

从2.5开始,Log4j支持自定义删除操作会在每次翻转时执行.

Since 2.5, Log4j supports a custom Delete action that is executed on every rollover.

您可以控制通过以下任意组合删除哪些文件:

You can control which files are deleted by any combination of:

  1. 名称(匹配全局 regex )
  2. 计数(仅保留最近的3个")
  3. 大小(仅保留最大为500MB的最新文件")
  1. Name (matching a glob or a regex)
  2. Age ("delete if 14 days old or older")
  3. Count ("keep only the most recent 3")
  4. Size ("keep only the most recent files up to 500MB")

需要更精细地控制要删除哪些文件的用户,可以使用任何受支持的JSR-223脚本语言来指定脚本条件.

Users who need even more fine-grained control over which files to delete can specify a script condition using any supported JSR-223 scripting language.

请查看文档,其中包含三个可能有用的完整示例.

Please check out the documentation, it has three full examples that may be useful.

顺便说一句,请注意,您可以在以下位置压缩日志文件滚动以使其占用更少的磁盘空间.

As an aside, note that you can compress log files on rollover to make them take up less disk space.

最后,要小心!无法恢复以这种方式删除的文件.:-)

Finally, be careful! There is no way to recover files deleted this way. :-)

这篇关于SizeBasedTriggeringPolicy限制不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆