【发布时间】:2011-05-06 12:33:10
【问题描述】:
我发现 a useful article 解释了如何让 Jersey 使用 SLF4J 而不是 JUL。现在我的单元测试看起来像(并且效果很好):
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>
它运行良好,但我不想在每个单元测试中都进行相同的配置。这是一个明显的代码重复,我想避免。我怎样才能更有效地做到这一点?
ps。可能无法优化上面的代码,我已经尽力了?
【问题讨论】:
-
只是对 initLogger() 的增强,你可以使用这个:SLF4JBridgeHandler.removeHandlersForRootLogger(); java.util.logging.LogManager.getLogManager().reset(); SLF4JBridgeHandler.install();
标签: java logging log4j jersey slf4j