Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I control logging in 3rd party libraries

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?

like image 246
Martin Avatar asked Oct 18 '22 21:10

Martin


1 Answers

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 .

like image 71
dur Avatar answered Oct 21 '22 14:10

dur