[英]How do I control logging in 3rd party libraries
I have a Tomcat server running a Spring-based servlet. 我有一个运行基于Spring的servlet的Tomcat服务器。
I've set up [project root]/src/log4j.properties
file as below: 我已经设置了[project root]/src/log4j.properties
文件,如下所示:
# 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
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. 例如,即使我将log4j.logger.org.apache=WARN
添加到我的log4j.properties,我仍然会在Tomcat启动期间从org.apache.*
获取INFO日志到Eclipse控制台错误流。
I'm using slf4j-api
and slf4j-log4j
jars, obtained through Maven. 我正在使用通过Maven获得的slf4j-api
和slf4j-log4j
罐子。
How can I take control of logging levels and targets outside my own code? 如何控制我自己的代码之外的日志记录级别和目标?
Some libraries use other logging frameworks like java.util.logging
. 一些库使用其他日志框架,如java.util.logging
。
You could redirect logging with SLF4J , see SLF4J - Bridging legacy APIs : 您可以使用SLF4J重定向日志记录,请参阅SLF4J - 桥接旧版API :
Redirection for Jakarta Commons Logging: Jakarta Commons Logging的重定向:
To ease migration to SLF4J from JCL, SLF4J distributions include the jar file
jcl-over-slf4j.jar
. 为了便于从JCL迁移到SLF4J,SLF4J发行版包括jar文件jcl-over-slf4j.jar
。 This jar file is intended as a drop-in replacement for JCL version 1.1.1. 此jar文件旨在作为JCL 1.1.1版的替代品。 It implements the public API of JCL but using SLF4J underneath, hence the name "JCL over SLF4J." 它实现了JCL的公共API,但在下面使用了SLF4J,因此名称为“JCL over SLF4J”。
Redirection for java.util.Logging
( SLF4J API ): java.util.Logging
重定向( SLF4J API ):
Installation via logging.properties configuration file: 通过logging.properties配置文件安装:
// register SLF4JBridgeHandler as handler for the jul root logger //将SLF4JBridgeHandler注册为jul根记录器的处理程序
handlers = org.slf4j.bridge.SLF4JBridgeHandler handlers = org.slf4j.bridge.SLF4JBridgeHandler
For configuration of java.util.Logging
see JUL API . 有关java.util.Logging
配置,请参阅JUL API 。
Some libraries like Apache CXF supports more than one logging framework, see Apache CXF - Debugging and Logging . Apache CXF等一些库支持多个日志框架,请参阅Apache CXF - 调试和日志记录 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.