How to set log4j level on command line?

I want to add some log.debug instructions to the class I'm working on, and I would like to see this in the output when the test runs. I would like to override the log4j properties on the command line, for example:

-Dlog4j.logger.com.mypackage.Thingie=DEBUG 

I often do this. I am particularly interested in the way to pass this on the command line. I know how to do this with a configuration file, and this is not suitable for my workflow.

+61
java log4j
Aug 19 '11 at 19:56
source share
7 answers

log4j does not support this directly.

Since you do not need a configuration file, most likely you are using a software configuration. I would suggest that you study all the properties of the system and explicitly program what you want based on this.

+12
Aug 19 '11 at 20:01
source share

As part of your jvm arguments, you can set -Dlog4j.configuration=file:"<FILE_PATH>" . Where FILE_PATH is the path to your log4j.properties file.

Note that with log4j2, the new system variable used as log4j.configurationFile , and you insert the actual file path (i.e. without the file: prefix), and it will automatically load the factory based on the configuration file extension:

 -Dlog4j.configurationFile=/path/to/log4jconfig.{ext} 
+44
Aug 19 '11 at 8:11 a.m.
source share

These answers actually dissuaded me from trying the simplest possible thing! Just specify the threshold for the appender (say "console") in your log4j.configuration , for example:

 log4j.appender.console.threshold=${my.logging.threshold} 

Then, on the command line, specify the system property -Dlog4j.info -Dmy.logging.threshold=INFO . I assume that any other property can be parameterized in this way, but this is the easiest way to increase or decrease the level of logging on a global scale.

+28
Jul 03 '15 at 13:49
source share

With Log4j2, this can be achieved using the following utility method added to your code.

 private static void setLogLevel() { if (Boolean.getBoolean("log4j.debug")) { Configurator.setLevel(System.getProperty("log4j.logger"), Level.DEBUG); } } 

You need these imported

 import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.config.Configurator; 

Now call the setLogLevel method in your main () or any relevant command line options pass -Dlog4j.logger=com.mypackage.Thingie and -Dlog4j.debug=true .

+11
02 Feb '17 at 3:01
source share

Based on a proposal by Thorbjørn Ravn Andersens, I wrote code that does this work

Add the following early path to the main method, and now you can set the log level from the comand line. This was checked in my project, but I am new to log4j and may have made a mistake. If so, please correct me.

  Logger.getRootLogger().setLevel(Level.WARN); HashMap<String,Level> logLevels=new HashMap<String,Level>(); logLevels.put("ALL",Level.ALL); logLevels.put("TRACE",Level.TRACE); logLevels.put("DEBUG",Level.DEBUG); logLevels.put("INFO",Level.INFO); logLevels.put("WARN",Level.WARN); logLevels.put("ERROR",Level.ERROR); logLevels.put("FATAL",Level.FATAL); logLevels.put("OFF",Level.OFF); for(String name:System.getProperties().stringPropertyNames()){ String logger="log4j.logger."; if(name.startsWith(logger)){ String loggerName=name.substring(logger.length()); String loggerValue=System.getProperty(name); if(logLevels.containsKey(loggerValue)) Logger.getLogger(loggerName).setLevel(logLevels.get(loggerValue)); else Logger.getRootLogger().warn("unknown log4j logg level on comand line: "+loggerValue); } } 
+6
Apr 10 '13 at 10:10
source share

Based on @lijat, here is a simplified implementation. In my spring application, I just load it as a bean.

 public static void configureLog4jFromSystemProperties() { final String LOGGER_PREFIX = "log4j.logger."; for(String propertyName : System.getProperties().stringPropertyNames()) { if (propertyName.startsWith(LOGGER_PREFIX)) { String loggerName = propertyName.substring(LOGGER_PREFIX.length()); String levelName = System.getProperty(propertyName, ""); Level level = Level.toLevel(levelName); // defaults to DEBUG if (!"".equals(levelName) && !levelName.toUpperCase().equals(level.toString())) { logger.error("Skipping unrecognized log4j log level " + levelName + ": -D" + propertyName + "=" + levelName); continue; } logger.info("Setting " + loggerName + " => " + level.toString()); Logger.getLogger(loggerName).setLevel(level); } } } 
+2
May 31 '13 at 17:42
source share

In my fairly standard installation, I saw well the following work when passing as a VM option (command line before the class in Java or VM option in the IDE):

 -Droot.log.level=TRACE 
+1
Aug 23
source share



All Articles