Skip to content
Advertisement

How to set java.util.logging.ConsoleHandler.level from command line arguments?

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement