干净地强制 Log4j RollingFileAppender 在午夜后不久滚动?
Log4j RollingFileAppender 的正常行为是在第一条日志消息发生在不同的一天时滚动,但有些人会因为每个日期的空日志文件而感到温暖和模糊,即使什么也没有发生。有没有办法强制它在午夜之后滚动而不向日志写入虚拟消息?
The normal behavior of the Log4j RollingFileAppender is to roll when the first log message occurs on a different day, but some feel warm and fuzzy with empty logfiles for each date, even if nothing occurred. Is there a way to force it to roll after midnight without writing dummy messages to the log?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我非常仔细地查看了这段代码 - 简单的答案是“不”。翻转作为 Appender 上 doAppend() 流程的一部分被触发 - 触发它的唯一方法是记录一些内容。
你可以用 cron 来伪造这个:只需让 cron 脚本在明天 11:58 点触摸文件即可。这将为您提供您正在寻找的空日志文件行为。
下面是实现翻转功能的代码:
I've looked at this code very closely - the simple answer is 'no'. The rollover is triggered as part of the doAppend() flow on the Appender - the only way to trigger it is to log something.
You could fake this with cron: just have a cron script touch the file for tomorrow at like 11:58. That will get you the empty logfile behavior you're looking for.
Here's the code that implements the rollover function: