2010-10-28 85 views
1

我在我的應用程序中使用Log4j,並且使用Log4j的庫也將它們的日誌輸出到我創建的日誌文件中。我已經創建了appender將一個庫的日誌重定向到「other.log」文件,但是其他庫仍然記錄到我的主「info.log」文件中。Log4j - 排除某些類的日誌記錄

這是我的log4j.properties。請注意,最後,我爲圖書館alibrary.apackagemyproject.apackage創建了一個類別,以便圖書館日誌轉到一個appender,並將項目日誌轉到另一appender。

log4j.rootLogger=ALL,InfoAppender,OtherAppender 

# AdminFileAppender - used to log messages in the admin.log file. 
log4j.appender.InfoAppender=org.apache.log4j.FileAppender 
log4j.appender.InfoAppender.File=info.log 
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.InfoAppender.layout.ConversionPattern= %d{yyyy/MM/dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n 
log4j.appender.InfoAppender.Threshold=DEBUG 

log4j.appender.OtherAppender=org.apache.log4j.FileAppender 
log4j.appender.OtherAppender.File=other.log 
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.OtherAppender.layout.ConversionPattern= %d{yyyy/MM/dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n 
log4j.appender.OtherAppender.Threshold=ALL 

log4j.category.alibrary.apackage=DEBUG,OtherAppender 
log4j.additivity.com.mchange.v2=false 
log4j.category.myproject.apackage=ALL,InfoAppender 
log4j.additivity.trackme=false 

我仍然不斷收到雖然以下幾點:

在 「info.log建立」:

...Logs that I want to be here... 
Logs that I do not want to be here, that should go to "other.log". Ex.: 
2010/10/28 15:29:25,667 [main] DEBUG org.apache.jasper.compiler.JspRuntimeContext - Parent class loader is: [email protected] 
2010/10/28 15:29:25,668 [main] DEBUG org.apache.jasper.servlet.JspServlet - ... 
2010/10/28 15:29:25,668 [main] DEBUG org.apache.jasper.servlet.JspServlet - IMPORTANT: Do not modify the generated servlets 

在 「other.log」:

All the logs that I do not want in "info.log" are here. OK. 

我問題是:如何將所有不需要的日誌 - 即其他庫中的日誌 - 重定向到「other.log」?

回答

2

您希望使OtherAppender成爲rootLogger,並將所需的消息特別推送到InfoAppender中。

我會離開的appender定義相同,然後配置記錄器是這樣的:

log4j.rootLogger=ALL,OtherAppender 
log4j.category.alibrary.apackage=DEBUG # will default to OtherAppender 
log4j.additivity.com.mchange.v2=false 
log4j.category.myproject.apackage=ALL,InfoAppender # will go to both logs 
log4j.additivity.trackme=false 

將路由到一切OtherAppender和myproject.apackage只是東西放到InfoAppender。