1

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?

AutoTester999
  • 376
  • 4
  • 16
  • 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. –  Mar 21 '18 at 18:49
  • Yes. I am not able to over-ride which I thought would happen. Any clue why? – AutoTester999 Mar 21 '18 at 18:53
  • 1
    Why did you think you were overriding? Where did you see that being done / documented? – Andreas Mar 21 '18 at 18:59
  • 1
    To change logging options from command-line, tell Java to load a *different* logging configuration file, as shown [here](https://stackoverflow.com/a/960133/5221149). – Andreas Mar 21 '18 at 19:03

2 Answers2

2

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.

jmehrens
  • 9,782
  • 1
  • 33
  • 45
2

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: overrideLogLevels
you don't even need to include the containing java-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/java-utils.jar:${CLASSPATH} \
-Djava.util.logging.config.class=pl.morgwai.base.logging.JulConfig \
-Djava.util.logging.overrideLevel=,java.util.logging.ConsoleHandler \
-D.level=FINE \
-Djava.util.logging.ConsoleHandler.level=FINE \
${MY_JAVA_APP_MAINCLASS_AND_ARGUMENTS}
morgwai
  • 2,180
  • 3
  • 26
  • 28