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 propertyName = "java.util.logging.ConsoleHandler.level";
var cmdLineVal = System.getProperty(propertyName);
if (cmdLineVal != null) {
LogManager.getLogManager().updateConfiguration(
(key) -> (oldVal, newVal) ->
key.equals(propertyName) ? 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}