Reputation: 616
Current default global logging level is set to INFO in JRE_HOME/lib/logging.properties file.
I run the following from the command line to over-ride and set the level to FINE:
mvn test -Dtest=ABC -Djava.util.logging.ConsoleHandler.level=FINE
And, I use the below in my code:
logger.fine("Logging works for fine");
The above message doesn't get printed in the output.
If I change it to the below line, it prints successfully.
logger.info("Logging works for fine");
What am I missing?
Upvotes: 4
Views: 11894
Reputation: 2803
To add to the answer by jmehrens: if you are using java-9 or later, then a much easier way to override LogManager
's property, is to use LogManager.updateConfiguration(mapper) method.
So in the case of ConsoleHandler
's level:
final var LEVEL_PROPERTY = "java.util.logging.ConsoleHandler.level";
var cmdLineVal = System.getProperty(LEVEL_PROPERTY);
if (cmdLineVal != null) {
LogManager.getLogManager().updateConfiguration(
(key) -> (oldVal, newVal) ->
key.equals(LEVEL_PROPERTY) ? cmdLineVal : newVal
);
}
Note, that you cannot use updateConfiguration(...)
to add new properties: only modify the existing ones.
update: I've just written a simple function to make ad-hoc command-line changes to logging config easier: overrideLogLevelsWithSystemProperties (also supports adding new logging properties).
you don't even need to include the containing jul-utils
as a dependency of your project: just add it to your classpath (available in central) when starting your app and define your desired system properties:
java -cp /path/to/jul-utils.jar:${CLASSPATH} \
-Djava.util.logging.config.class=pl.morgwai.base.jul.JulConfigurator \
-Djava.util.logging.overrideLevel=,java.util.logging.ConsoleHandler,com.third.party.talkative.lib \
-D.level=FINE \
-Djava.util.logging.ConsoleHandler.level=FINE \
-Dcom.third.party.talkative.lib.level=SEVERE \
${MY_JAVA_APP_MAINCLASS_AND_ARGUMENTS}
Upvotes: 4
Reputation: 11045
The command switch -Djava.util.logging.ConsoleHandler.level=FINE
just adds a system property entry. This is not used or read by the logging API.
Instead, all of the logging properties are managed by the LogManager.
Here is a self contained program to show how you the LogManager
can change settings:
public class LogManagerTest {
public static void main(String[] arg) throws IOException {
read(LogManager.getLogManager(), create());
Handler h = new ConsoleHandler();
System.out.println(h.getLevel());
h.close();
}
private static Properties create() {
Properties props = new Properties();
props.setProperty("java.util.logging.ConsoleHandler.level",
"FINE");
return props;
}
private static void read(LogManager manager, Properties props) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream(512);
props.store(out, "No comment");
manager.readConfiguration(new ByteArrayInputStream(out.toByteArray()));
}
}
Like @Andreas pointed out, you are going to create a new properties file with the adjusted parameters and set the system property to have the LogManager
use the new properties file with your desired settings.
Upvotes: 3