java 如何为 JUnit 测试设置日志级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38566865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the Loglevel for a JUnit Test
提问by Ralph
I am using java logging in my classes.
我在我的课程中使用 java 日志记录。
Example:
例子:
public class MyClass {
private static Logger logger = Logger.getLogger(MyClass.class.getName());
....
}
When I am writing a JUnit Test for that class, I would like to set the Loglevel to 'FINE'. I tried:
当我为该类编写 JUnit 测试时,我想将 Loglevel 设置为“FINE”。我试过:
@Before
public void setup() throws PluginException {
Logger.getGlobal().setLevel(Level.FINE);
....
}
But this has no effect. How can I control the loglevel in a JUnit Test when using Java Logging? I am running my tests using Maven.
但这没有效果。使用 Java 日志记录时,如何控制 JUnit 测试中的日志级别?我正在使用 Maven 运行我的测试。
回答by vallismortis
This is the simplest, most generic way I've found to set the java.util.logging
level for JUnit tests.
这是我发现的java.util.logging
为 JUnit 测试设置级别的最简单、最通用的方法。
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
public class MyTest
{
@BeforeClass
public static void beforeClass()
{
System.setProperty("java.util.logging.config.file", ClassLoader.getSystemResource("logging.properties").getPath());
}
@Test
public void test00a()
{
Logger.getLogger(MyTest.class.getName()).log(Level.FINE, "J.U.L. test");
}
}
In src/test/resources
create logging.properties
(.level
must be the first line):
在src/test/resources
创建logging.properties
(.level
必须是第一行):
.level=FINEST
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
When you run the unit test(s) in this class, you should see:
当您在此类中运行单元测试时,您应该看到:
May 01, 2017 12:17:12 PM MyTest test00a
FINE: J.U.L. test
2017 年 5 月 1 日下午 12:17:12 MyTest test00a
罚款:7 月测试
回答by piepera
To solve this problem, I defined a custom JUnit 4 rule to disable logs for a specific test:
为了解决这个问题,我定义了一个自定义的 JUnit 4 规则来禁用特定测试的日志:
@RequiredArgsConstructor
public class LogLevelRule extends TestWatcher {
private Level oldLevel;
private final Class clazz;
private final String levelString;
@Override
protected void starting(Description description) {
oldLevel = Logger.getLogger(clazz).getLevel();
Logger.getLogger(clazz).setLevel(Level.toLevel(levelString));
}
@Override
protected void finished(Description description) {
Logger.getLogger(clazz).setLevel(oldLevel);
}
}
This can then be enabled by declaring a @Rule
of @ClassRule
in the particular test:
然后可以通过在特定测试中声明 a @Rule
of来启用它@ClassRule
:
@ClassRule
public static LogLevelRule logLevelRule = new LogLevelRule(MyClass.class, "OFF");
回答by TMichelsen
Logger.getLogger() documentation:
Logger.getLogger() 文档:
Find or create a logger for a named subsystem. If a logger has already been created with the given name it is returned. Otherwise a new logger is created.
为命名子系统查找或创建记录器。如果已经使用给定名称创建了记录器,则返回该记录器。否则会创建一个新的记录器。
In your case, Logger.getLogger() with the same name gives you the same logger-instance. Therefore, you can get the logger and set its level with
在您的情况下,具有相同名称的 Logger.getLogger() 为您提供相同的记录器实例。因此,您可以获取记录器并设置其级别
@Before
public void setup() throws PluginException {
Logger.getLogger(MyClass.class.getName()).setLevel(Level.FINE);
...
}
Just make sure to set it back in the @After-method later.
请确保稍后在@After-method 中将其重新设置。