开发者

Is there a command line option for setting the default log level in Java

开发者 https://www.devze.com 2022-12-19 00:13 出处:网络
Can I do something along the lines of: -Djava.util.logging.logl开发者_运维技巧evel=FINE Obviously that doesn\'t work, but you get the idea.Is there anything like that?Or am I forced to create a pro

Can I do something along the lines of:

-Djava.util.logging.logl开发者_运维技巧evel=FINE

Obviously that doesn't work, but you get the idea. Is there anything like that? Or am I forced to create a properties file?


You can even pass your log Level as a user defined property.

-DmyProp.logLevel=FINE

In your code:

String logLevel = System.getProperties("myProp.logLevel");

But I have the idea that your are looking for a more "built-in" and automatically handled property, right? AFAIK, it doesn't exist, but maybe I'm wrong.


Generally java.util.logging does not use system properties to configure itself (there are few exceptions, like java.util.logging.SimpleFormatter.format). Instead, global LogManager singleton instance is responsible for configuring the logging subsystem and by default it loads properties from ${JAVA_HOME}/conf/logging.properties. There are few ways to tweak this behavior:

  • provide java.util.logging.config.file system property with a path to an alternative logging config properties file.
  • provide java.util.logging.config.class system property with a class name that completely overrides configuration process (usually by providing a custom InputStream with logging config properties to readConfiguration(is)).
  • use LogManager.updateConfiguration(mapper) method (java 9+) to override selected properties only. Note that this method can only override existing properties, but cannot add new ones (for that, a way more cumbersome updateConfiguration(inputStream, mapper) would need to be used).

So in case of overriding the global root log-level, it's probably easiest to use updateConfiguration(mapper) somewhere at the beginning of your main method. LogManager uses .level property (level of the "root" logger) as a default for all its child loggers, so using the java.util.logging.loglevel system property from the example, it would be like this:

var cmdLineVal = System.getProperty("java.util.logging.loglevel");
if (cmdLineVal != null) {
    LogManager.getLogManager().updateConfiguration(
            (key) -> (oldVal, newVal) ->
                    key.equals(".level") ? cmdLineVal : newVal);
}

Note that in case of the default logging config, the above is not sufficient for any FINE (or lower) log entries to be output on the console, as ConsoleHandler by default outputs INFO and higher. To change this, you need to override also java.util.logging.ConsoleHandler.level logging config property. If you want to use the same system property for this again, then it would be like this:

var cmdLineVal = System.getProperty("java.util.logging.loglevel");
if (cmdLineVal != null) {
    LogManager.getLogManager().updateConfiguration(
        (key) -> (oldVal, newVal) -> {
            if (
                key.equals(ConsoleHandler.class.getName() + ".level")
                || key.equals(".level")
            ) {
                return cmdLineVal;
            } else {
                return newVal;
            }
        }
    );
}

As a final note, I've recently written a simple helper function to make ad-hoc command-line changes to logging config easier: overrideLogLevels.
As mentioned by @TWiStErRob in the comment, 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}


you can configure your code to set the level based on an envrioment variable :

String sLoglevel= System.getenv("LOGLEVEL");  
int ilevel = loglevel.parseInt(sLoglevel);
//set the log level based on retrieved value 
0

精彩评论

暂无评论...
验证码 换一张
取 消