Reputation: 1615
I have the same problem as this: How to set the log level to DEBUG during Junit tests? - except I'm working with java.util.logging
, not Log4j.
Specifically, I'm converting an existing (Maven-built) application from Log4j to java.util.logging
, in order to comply with a customer requirement (not negotiable). Everything uses SLF4J, so code changes are minimal. There are over a thousand test classes, spread across a few modules; we currently have a log4j.properties
file in the src/test/resources
of those modules to customize the log output of the tests.
I think it's important for log output to be easily customizable for tests: if a developer breaks a test and wants to read the log output, I'd prefer they locally tweak the log config than start messing with the (carefully chosen) log levels in the code, leading to "everything logged at info" syndrome. Note that I want the test log config to apply to "the test+application code when executed under test conditions".
I don't want to add log settings to each and every Test class (as suggested here: How to set the Loglevel for a JUnit Test), because of the large number of tests. It seems like the wrong solution. I also don't want to go editing the JVM's logging.properties
file, as that would affect everything. This app isn't the only thing I do. I'd also have to get everyone else to make the same changes.
The existing system works because Log4j picks up /log4j.properties
from the classpath. Is there an equivalent mechanism for java.util.logging
?
Current thinking: add a logging.properties
file to replace the existing config, and try and hack the Maven config to set the java.util.logging.config.file
param to point at it. Is there a neater way?
Upvotes: 4
Views: 6398
Reputation: 7519
A project-wide src/test/resources/logging.properties
can be set specifically for unit testing via the java.util.logging.config.file
System property using a single line in your @BeforeClass
method:
System.setProperty("java.util.logging.config.file", ClassLoader.getSystemResource("logging.properties").getPath());
Upvotes: 4
Reputation: 11085
Is there a neater way?
That is probably the most reliable method to set the logging configuration.
Is there an equivalent mechanism for
java.util.logging
?
Configuration options are listed in the java.util.logging.LogManager documentation. You can:
java.util.logging.config.file
property and use a properties file.java.util.logging.config.class
property and create a class file with a no argument constructor to execute any code you desire to manually configure the loggers.java.util.logging.config.class
property too.Upvotes: 2