如何让 Jersey 使用 SLF4J 而不是 JUL? [英] How to make Jersey to use SLF4J instead of JUL?

查看:31
本文介绍了如何让 Jersey 使用 SLF4J 而不是 JUL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一篇有用的文章 这解释了如何让 Jersey 使用 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 包括这些依赖项:

My pom.xml includes these dependencies:

<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).

这篇关于如何让 Jersey 使用 SLF4J 而不是 JUL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆