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

前端 未结 1 1556
深忆病人
深忆病人 2020-11-29 05:03

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 refacto

相关标签:
1条回答
  • 2020-11-29 05:06

    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>
    
    0 讨论(0)
提交回复
热议问题