如何让泽西使用SLF4J代替JUL? [英] How to make Jersey to use SLF4J instead of JUL?
问题描述
我发现一篇有用的文章这解释了如何让泽西使用SLF4J而不是JUL。现在我的单元测试看起来像(并且它完美地运行):
I've found a useful article that explains how to make Jersey to use SLF4J instead of JUL. Now my unit test looks like (and it works perfectly):
public class FooTest extends JerseyTest {
@BeforeClass
public static void initLogger() {
java.util.logging.Logger rootLogger =
java.util.logging.LogManager.getLogManager().getLogger("");
java.util.logging.Handler[] handlers = rootLogger.getHandlers();
for (int i = 0; i < handlers.length; i++) {
rootLogger.removeHandler(handlers[i]);
}
org.slf4j.bridge.SLF4JBridgeHandler.install();
}
public FooTest() {
super("com.XXX");
}
@Test
public void testSomething() throws Exception {
// ...
}
}
我的 pom.xml
包含这些依赖项:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
它完美无缺,但我不想在每个单元测试中进行相同的配置。这是一个明显的代码重复,我想避免。我怎样才能更有效地做到这一点?
It works perfectly, but I don't want to make the same configuration in every unit test. It's an obvious code duplication, which I would like to avoid. How can I do this more effectively?
ps。也许不可能优化上面的代码而且我能做到最好?
ps. Maybe it's not possible to optimize the code above and I'm doing the best I can?
推荐答案
最好的办法通过自定义听众。在JSF servlet之前初始化它应该在 contextInitialized(ServletContextEvent)
。
The best way to do it is through a custom Listener. Being initialized before JSF servlet it should configure jul-to-slf4j bridge in contextInitialized(ServletContextEvent)
.
这篇关于如何让泽西使用SLF4J代替JUL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!