I have implemented a simple unit-test in TestNG to test a message exchange. When I'm debugging the test I want to run all loggers (java.util.logging) to run with the log-level FINEST
. In order to achieve this, I have defined a @BeforeClass
method which switches all loggers to the intended log-level. Is there any possibility to automatically enable the fine-grained-Loggers when starting the java-vm in debug mode (via IntelliJ). I thought about influencing the enabled
-property of the BeforeClass
-Annotation, but have no idea whether and how this is possible.
@BeforeClass(enabled = false)
public void beforeClass()
{
Logger rootLogger = LogManager.getLogManager().getLogger("");
rootLogger.setLevel(Level.FINEST);
for (Handler h : rootLogger.getHandlers()) {
h.setLevel(Level.FINEST);
}
}
You could use ManagementFactory.getRuntimeMXBean().getInputArguments()
to know whether you are in debug mode in runtime, and set log level accordingly.
In intelliJ, the following code:
System.out.println("TEST: " +
ManagementFactory.getRuntimeMXBean().getInputArguments());
Prints the following output in debug mode:
TEST: [-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:40770,suspend=y,server=n, -ea, -Didea.test.cyclic.buffer.size=1048576, -javaagent:/opt/idea-IC-181.4203.550/plugins/Groovy/lib/agent/gragent.jar, -javaagent:/opt/idea-IC-181.4203.550/lib/rt/debugger-agent.jar=file:/tmp/capture.props, -Dfile.encoding=UTF-8]
and the following output in non-debug mode:
TEST: [-ea, -Didea.test.cyclic.buffer.size=1048576, -javaagent:/opt/idea-IC-181.4203.550/lib/idea_rt.jar=46442:/opt/idea-IC-181.4203.550/bin, -Dfile.encoding=UTF-8]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.