本文介绍了使用SLF4J发送/重定向/路由java.util.logging.Logger(JUL)到Logback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对 java.util.logging.Logger 进行典型调用,并使用SLF4J将其路由到Logback?这将是很好的,因为我不必逐行重构旧的jul代码。

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,我们有这条线:

EG, say we have this line:

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

将其配置为通过SLF4J调用会很好。

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

推荐答案

这很容易而且不再是性能问题了。

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

将jul-to-slf4j.jar添加到类路径中。或者通过maven依赖:

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>

如果你没有logging.properties(对于java.util.logging),请将此添加到你的引导代码:

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

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

如果你有logging.properties(并希望保留它),请将其添加到它:

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

handlers = org.slf4j.bridge.SLF4JBridgeHandler

为了避免性能损失,请将此contextListener添加到logback.xml(从logback版本0.9.25开始):

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>

这篇关于使用SLF4J发送/重定向/路由java.util.logging.Logger(JUL)到Logback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:58