开发者

Log4j - Excluding the logging of some classes

开发者 https://www.devze.com 2023-01-22 16:20 出处:网络
I am using Log4j in my application, and the libraries that I use that also use Log4j are also outputting their logs to the log files that I create. I have already created appenders to redirect the log

I am using Log4j in my application, and the libraries that I use that also use Log4j are also outputting their logs to the log files that I create. I have already created appenders to redirect the logs of one library to an "other.log" file, but the other libraries keep logging to my main "info.log" file.

This is my log4j.properties. Notice that in the end, I create a category for the library alibrary.apackage and a category for myproject.apackage, so that the library logs go to one appender and the project logs go to another 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

I still keep getting the following though:

In the "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: ContextLoader@null
2010/10/28 15:29:25,668 [main] DEBUG org.apache.jasper.servlet.JspServlet  - ...开发者_JAVA百科
2010/10/28 15:29:25,668 [main] DEBUG org.apache.jasper.servlet.JspServlet  - IMPORTANT: Do not modify the generated servlets

In the "other.log":

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

My question is: How do I redirect all the unwanted logs - that is, logs from other libraries - to the "other.log"?


You want to make OtherAppender the rootLogger and specifically push the messages you want into InfoAppender.

I'd leave the appender definitions the same and then configure the loggers like this:

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

That will route everything into OtherAppender and just things from myproject.apackage into InfoAppender.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号