log4j - DailyRollingFileAppender,覆盖subAppend()

时间:2011-10-10 02:55:23

标签: log4j

我是log4j的DailyRollingFileAppender类的新手,我想使用它来执行日志文件的每日轮换,同时还想在每次触发事件触发事件时手动修改日志文件。

例如,我想在日志文件中始终将值增加一个“TOTAL COUNT:”。我怎么能这样做?

日志内容示例:

07 Oct 2011 16:57:51 [INFO ] - Failed
07 Oct 2011 16:57:51 [WARN ] - Failed
07 Oct 2011 16:57:51 [ERROR] - Successful
07 Oct 2011 16:57:51 [FATAL] - Failed
07 Oct 2011 16:57:52 [DEBUG] - Successful
07 Oct 2011 16:57:52 [INFO ] - Failed
07 Oct 2011 16:57:52 [WARN ] - Failed
07 Oct 2011 16:57:52 [ERROR] - Successful
07 Oct 2011 16:57:52 [FATAL] - Failed
07 Oct 2011 16:57:53 [DEBUG] - Successful
07 Oct 2011 16:57:53 [INFO ] - Failed
07 Oct 2011 16:57:53 [WARN ] - Failed
07 Oct 2011 16:57:53 [ERROR] - Successful
07 Oct 2011 16:57:53 [FATAL] - Failed
07 Oct 2011 16:57:54 [DEBUG] – Successful

TOTAL COUNT: 15

1 个答案:

答案 0 :(得分:0)

subAppend()中的DailyRollingFileAppender似乎是最重要的。在调用super.subAppend()时,您还必须谨慎,因为WriterAppender会像这样实现它:

protected void subAppend(LoggingEvent event) {
    this.qw.write(this.layout.format(event));
    // ...
}

并且您不希望使用“TOTAL COUNT”行进行布局。

在我的subAppend()中,我完全复制了D.R.F.Appender所做的事情,并补充道:

  • 行计数逻辑,如果不在其他地方,
  • 检查是否应打印特殊行的条件,然后将其格式化并直接打印到qw