按大小和时间滚动日志 [英] Rolling logs by both size and time

查看:154
本文介绍了按大小和时间滚动日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 RollingFileAppender log4j 1.2.16 ,它会在日志文件达到一定大小时滚动它们。现在,我想在日期达到一定大小时滚动日志文件。因此,每天会有一个或多个日志文件。

I use RollingFileAppender of log4j 1.2.16, which rolls log files, when they reach a certain size. Now I would like to roll log files daily and when they reach a certain size. Thus there will be one or more log files per day.

例如,

myapp.log
myapp-17.12.2013.log
myapp-16.12.2012.log
myapp-16.12.2012.1.log
myapp-16.12.2012.2.log

是否有现成的appender,它已经完成了吗?

Is there an off-the-shelf appender, which does it already?

推荐答案

确实有两种选择:


  1. 使用LogBack及其大小和时间触发策略: http://logback.qos。 ch / manual / appenders.html#SizeAndTimeBasedFNATP

  2. 这里有Log4J的TimeAndSizeRollingAppender: http://www.simonsite.org.uk/

  1. use LogBack with its size and time triggering policy: http://logback.qos.ch/manual/appenders.html#SizeAndTimeBasedFNATP
  2. there is TimeAndSizeRollingAppender for Log4J from here: http://www.simonsite.org.uk/

请记住,两者都是选项使用文件重命名。如果有另一个脚本自动移动这些文件,请仔细考虑这一点。当两个进程处理同一个文件时,文件重命名是有风险的。

Keep in mind that both options use file renames. Consider this carefully if there's another script automatically moving these files. File rename is risky when two processes deal with the same file.

我的建议是直接写入模式中的不可变日志文件名:myapp- {dd.MM。 YYYY}。{X}日志。那样滚动只是关闭一个文件并打开一个新文件。没有重命名。没有背景线程。

My suggestion is to directly write to immutable log file name in the pattern: myapp-{dd.MM.yyyy}.{X}.log. That way "rolling" is simply closing one file and opening a new one. No renames. No background threads.

这篇关于按大小和时间滚动日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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