Log4j - 排除某些类的日志记录

发布于 2024-09-29 10:02:01 字数 1902 浏览 3 评论 0原文

我在我的应用程序中使用 Log4j,并且我使用的也使用 Log4j 的库也将其日志输出到我创建的日志文件中。我已经创建了附加程序来将一个库的日志重定向到“other.log”文件,但其他库继续记录到我的主“info.log”文件。

这是我的 log4j.properties。请注意,最后,我为库 alibrary.apackage 创建了一个类别,并为 myproject.apackage 创建了一个类别,以便库日志转到一个附加程序和项目日志转到另一个附加程序。

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: ContextLoader@null
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”:日志”?

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  - ...
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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-10-06 10:02:02

您想让OtherAppender成为rootLogger,并专门将您想要的消息推送到InfoAppender中。

我将保留附加程序定义不变,然后像这样配置记录器:

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 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文