4

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?

5
  • clearly you are not over riding the logging at the command-line and it's behaving as if it's set to INFO, which you said is the default. Commented Mar 21, 2018 at 18:49
  • Yes. I am not able to over-ride which I thought would happen. Any clue why? Commented Mar 21, 2018 at 18:53
  • 1
    Why did you think you were overriding? Where did you see that being done / documented? Commented Mar 21, 2018 at 18:59
  • 1
    To change logging options from command-line, tell Java to load a different logging configuration file, as shown here. Commented Mar 21, 2018 at 19:03
  • related: stackoverflow.com/questions/2160471/… Commented Jun 2, 2023 at 22:57

2 Answers 2

4

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}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.