How to make Jersey to use SLF4J instead of JUL?(如何让 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何让 Jersey 使用 SLF4J 而不是 JUL?
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
