本文介绍了log4j2:如何强制滚动文件追加程序翻转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,log4j2中的RollingFileAppender不会在指定时间(例如-在一个小时结束时)滚动,而是在超过时间阈值后到达的第一个日志事件.

To my best knowledge, RollingFileAppender in log4j2 will not roll over at the specified time (let's say - at the end of an hour), but at the first log event that arrives after the time threshold has been exceeded.

是否有一种触发事件的方法,一方面会导致文件翻转,另一方面却不会附加到日志中(或者会附加一些琐碎的内容,例如空字符串)?

Is there a way to trigger an event, that on one hand will cause the file to roll over, and on another - will not append to the log (or will append something trivial, like an empty string)?

推荐答案

不存在任何(内置)方法.没有后台线程监视过渡时间.

No there isn't any (built-in) way to do this. There are no background threads monitoring rollover time.

您可以创建一个实现org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy的log4j2插件(有关示例代码,请参见内置的TimeBasedTriggeringPolicy和SizeBasedTriggeringPolicy类.)

You could create a log4j2 plugin that implements org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy (See the built-in TimeBasedTriggeringPolicy and SizeBasedTriggeringPolicy classes for sample code.)

如果配置了自定义触发策略,log4j2将检查每个日志事件是否应触发翻转(因此在实现isTriggeringEvent方法时要格外小心,以免影响性能).请注意,要提取您的自定义插件,您需要在log4j2.xml文件的Configuration元素的packages属性中指定类的包.

If you configure your custom triggering policy, log4j2 will check for every log event whether it should trigger a rollover (so take care when implementing the isTriggeringEvent method to avoid impacting performance). Note that for your custom plugin to be picked up, you need to specify the package of your class in the packages attribute of the Configuration element of your log4j2.xml file.

最后,如果这对您来说很好,并且您认为您的解决方案也可能对其他人有用,请考虑将您的自定义触发策略贡献回log4j2代码库.

Finally, if this works well for you and you think your solution may be useful to others too, consider contributing your custom triggering policy back to the log4j2 code base.

这篇关于log4j2:如何强制滚动文件追加程序翻转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:30