log4j:一个类;同一水平;两个输出文件

时间:2009-10-01 21:16:34

标签: java log4j

我似乎没有在Additions.log或Deletions.log中获得任何INFO级别的消息,即使我看到日志行在调试器中执行。这是我的log4j.properties文件:

log4j.file.home=.

log4j.rootLogger=INFO, dest1
log4j.rootCategory=INFO, dest1
log4j.logger.org.hibernate=ERROR

log4j.category.dest1=INFO
log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} %-8p %c [%t] - %m (%l)%n

#Log items that are being added
log4j.logger.Additions=INFO
log4j.additivity.Additions=false
log4j.appender.Additions=org.apache.log4j.RollingFileAppender
log4j.appender.Additions.File=${log4j.file.home}/Additions.log
log4j.appender.Additions.MaxFileSize=10000KB
log4j.appender.Additions.MaxBackupIndex=10
log4j.appender.Additions.layout=org.apache.log4j.PatternLayout
log4j.appender.Additions.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} %-8p %c [%t] - %m (%l)%n

#Log items that are being removed
log4j.logger.Deletions=INFO
log4j.additivity.Deletions=false
log4j.appender.Deletions=org.apache.log4j.RollingFileAppender
log4j.appender.Deletions.File=${log4j.file.home}/Deletions.log
log4j.appender.Deletions.MaxFileSize=10000KB
log4j.appender.Deletions.MaxBackupIndex=10
log4j.appender.Deletions.layout=org.apache.log4j.PatternLayout
log4j.appender.Deletions.layout.ConversionPattern=%-22d{dd/MMM/yyyy HH:mm:ss} %-8p %c [%t] - %m (%l)%n 

这是从课堂上获取我的记录器:

private static Logger addLog = Logger.getLogger("Additions");
private static Logger deleteLog = Logger.getLogger("Deletions");

我还需要解决哪些问题来解决这个问题?

2 个答案:

答案 0 :(得分:6)

你把记录器与Appender混淆了。您已经定义了名为Additions and Deletions的Loggers,并且您已经定义了具有相同名称的Appender,但您需要将这两者相关联。仅仅给它们相同的名字是不够的。

我建议您将appender重命名为与Loggers不同,以避免这种混淆。然后,您需要将追加器分配给记录器,如下所示:

log4j.logger.Additions=INFO, Additions
log4j.logger.Deletions=INFO, Deletions

最后,我建议您考虑从属性格式转换为XML格式。在我看来,这是一个非常可读的,而且显而易见的是其他一切以及结构是什么。

答案 1 :(得分:1)

将此与我自己的log4j多文件写入配置相比较,我建议:

  1. 移动log4j.logger和log4j.additivity行,使它们位于log4j.appender语句之后。
  2. 修改log4j.logger以指定appender,如“log4j.logger.Additions = INFO,Additions”
  3. 当您的appender与记录器具有不同的名称时,它会变得更加清晰。这是我的一部分:

    log4j.appender.ChatLogs=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.ChatLogs.layout=org.apache.log4j.PatternLayout
    log4j.appender.ChatLogs.layout.ConversionPattern=%d{yyyy-MMM-dd HH:mm:ss:SSS}: %m%n
    log4j.appender.ChatLogs.File=logs/chats.log
    log4j.appender.ChatLogs.Threshold=INFO
    log4j.appender.ChatLogs.Priority=INFO
    log4j.appender.ChatLogs.DatePattern='.'yyyy-MM-dd
    
    log4j.logger.chatfilter.ChatFilterPlugin=info, ChatLogs
    log4j.additivity.chatfilter.ChatFilterPlugin=false