使用 SLF4J 发送/重定向/路由 java.util.logging.Logger (JUL) 到 Logback? [英] Send/redirect/route java.util.logging.Logger (JUL) to Logback using SLF4J?
问题描述
是否可以对 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.
将 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!