问题描述
我有一台运行基于Spring的servlet的Tomcat服务器.
I have a Tomcat server running a Spring-based servlet.
我已经设置了 [项目根目录]/src/log4j.properties
文件,如下所示:
I've set up [project root]/src/log4j.properties
file as below:
# Root logger option
log4j.rootLogger=WARN, stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{HH:mm:ss} %m [%c{3}:%L]%n
log4j.logger.com.martincarney.bugTracker=DEBUG
log4j.logger.com.martincarney.bugTracker.controller=ERROR
这正确地记录了我自己的代码,但似乎对我正在使用的各种库中的记录没有任何影响.例如,即使我添加 log4j.logger.org.apache = WARN org.apache.*
的INFO日志获取到Eclipse控制台错误流中.>转到我的log4j.properties.
This correctly logs my own code just fine, but doesn't seem to have any effect on logging from within the various libraries I'm using. For example, I still get INFO logs from org.apache.*
to the Eclipse console error stream during Tomcat startup, even if I add log4j.logger.org.apache=WARN
to my log4j.properties.
我正在使用通过Maven获得的 slf4j-api
和 slf4j-log4j
jar.
I'm using slf4j-api
and slf4j-log4j
jars, obtained through Maven.
如何在自己的代码之外控制日志记录级别和目标?
How can I take control of logging levels and targets outside my own code?
推荐答案
某些库使用其他日志记录框架,例如 java.util.logging
.
Some libraries use other logging frameworks like java.util.logging
.
您可以使用 SLF4J 重定向日志记录,请参见 SLF4J-桥接旧版API :
You could redirect logging with SLF4J, see SLF4J - Bridging legacy APIs:
雅加达公共记录日志的重定向:
Redirection for Jakarta Commons Logging:
java.util.Logging
的重定向( SLF4J API ):
//将SLF4JBridgeHandler注册为j.u.l的处理程序.根记录器
// register SLF4JBridgeHandler as handler for the j.u.l. root logger
handlers = org.slf4j.bridge.SLF4JBridgeHandler
handlers = org.slf4j.bridge.SLF4JBridgeHandler
有关 java.util.Logging
的配置,请参见 JUL API .
诸如Apache CXF之类的某些库支持多个日志记录框架,请参见 Apache CXF-调试和日志记录.
Some libraries like Apache CXF supports more than one logging framework, see Apache CXF - Debugging and Logging .
这篇关于如何控制第三方库中的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!