简体   繁体   English

使用SLF4J发送/重定向/路由java.util.logging.Logger(JUL)到Logback?

[英]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? 是否可以对java.util.logging.Logger进行典型调用,并使用SLF4J将其路由到Logback? This would be nice since I wouldn't have to refactor the old jul code line by line. 这将是很好的,因为我不必逐行重构旧的jul代码。

EG, say we have this line: EG,说我们有这条线:

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. 将其配置为通过SLF4J调用会很好。

It's very easy and not a performance issue anymore. 这很容易,而不是性能问题。

There are two ways documented in the SLF4J manual . SLF4J手册中记录了两种方法。 There are also precise examples in the Javadocs Javadocs中也有精确的例子

Add jul-to-slf4j.jar to your classpath. 将jul-to-slf4j.jar添加到类路径中。 Or through maven dependency: 或者通过maven依赖:

<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: 如果您没有logging.properties(对于java.util.logging),请将其添加到您的引导代码中:

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

If you have logging.properties (and want to keep it), add this to it: 如果您有logging.properties(并希望保留它),请将其添加到其中:

handlers = org.slf4j.bridge.SLF4JBridgeHandler

In order to avoid performance penalty, add this contextListener to logback.xml (as of logback version 0.9.25): 为了避免性能损失,请将此contextListener添加到logback.xml(从logback版本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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM