How do I control logging in 3rd party libraries

倾然丶 夕夏残阳落幕 提交于 2020-01-03 17:48:07

问题


I have a Tomcat server running a Spring-based servlet.

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

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.

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?


回答1:


Some libraries use other logging frameworks like java.util.logging.

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

Redirection for Jakarta Commons Logging:

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

Redirection for java.util.Logging (SLF4J API):

Installation via logging.properties configuration file:

// register SLF4JBridgeHandler as handler for the j.u.l. root logger

handlers = org.slf4j.bridge.SLF4JBridgeHandler

For configuration of java.util.Logging see JUL API.

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



来源:https://stackoverflow.com/questions/33890249/how-do-i-control-logging-in-3rd-party-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!