如何控制第三方库中的日志记录 [英] How do I control logging in 3rd party libraries

查看:78
本文介绍了如何控制第三方库中的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行基于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 重定向日志记录,请参见

You could redirect logging with SLF4J, see SLF4J - Bridging legacy APIs:

雅加达公共记录日志的重定向:

Redirection for Jakarta Commons Logging:

为了简化从JCL到SLF4J的迁移,SLF4J发行版包括jar文件 jcl-over-slf4j.jar .该jar文件旨在替代JCL 1.1.1版.它实现了JCL的公共API,但在下面使用了SLF4J,因此名称为"JCL over SLF4J".

To ease migration to SLF4J from JCL, SLF4J distributions include the jar file jcl-over-slf4j.jar. This jar file is intended as a drop-in replacement for JCL version 1.1.1. It implements the public API of JCL but using SLF4J underneath, hence the name "JCL over SLF4J."

java.util.Logging 的重定向( SLF4J API ):

通过logging.properties配置文件进行安装:

Installation via logging.properties configuration file:

//将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 的配置,请参见诸如Apache CXF之类的某些库支持多个日志记录框架,请参见 Apache CXF-调试和日志记录.

Some libraries like Apache CXF supports more than one logging framework, see Apache CXF - Debugging and Logging .

这篇关于如何控制第三方库中的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆