Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send/redirect/route java.util.logging.Logger (JUL) to Logback using SLF4J?

Is it possible to have a typical call to java.util.logging.Logger and have it route to Logback using SLF4J? This would be nice since I wouldn't have to refactor the old jul code line by line.

EG, say we have this line:

private static Logger logger = Logger.getLogger(MahClass.class.getName()); //... logger.info("blah blah blah"); 

It would be nice to configure this to call through SLF4J.

like image 532
Zombies Avatar asked May 16 '11 16:05

Zombies


1 Answers

It's very easy and not a performance issue anymore.

There are two ways documented in the SLF4J manual. There are also precise examples in the Javadocs

Add jul-to-slf4j.jar to your classpath. Or through maven dependency:

<dependency>     <groupId>org.slf4j</groupId>      <artifactId>jul-to-slf4j</artifactId>     <version>1.7.0</version> </dependency> 

If you don't have logging.properties (for java.util.logging), add this to your bootstrap code:

SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); 

If you have logging.properties (and want to keep it), add this to it:

handlers = org.slf4j.bridge.SLF4JBridgeHandler 

In order to avoid performance penalty, add this contextListener to logback.xml (as of logback version 0.9.25):

<?xml version="1.0" encoding="UTF-8"?> <configuration>      <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">         <!-- reset all previous level configurations of all j.u.l. loggers -->         <resetJUL>true</resetJUL>     </contextListener>       ...  </configuration> 
like image 192
Dejan Milosevic Avatar answered Sep 17 '22 21:09

Dejan Milosevic