Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set log4j level on the command line?

Tags:

java

log4j

I want to add some log.debug statements to a class I'm working on, and I'd like to see that in output when running the test. I'd like to override the log4j properties on the command line, with something like this:

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

I do this kind of thing frequently. I am specifically only interested in a way to pass this on the command line. I know how to do it with a config file, and that doesn't suit my workflow.

like image 978
Kevin Peterson Avatar asked Aug 19 '11 19:08

Kevin Peterson


People also ask

How do I change log level in log4j?

log4j provides you configuration file based level setting which sets you free from changing the source code when you want to change the debugging level. Following is an example configuration file which would perform the same task as we did using the log. setLevel(Level. WARN) method in the above example.

How do I change my log4j configuration?

The simplest is to put a log4j. xml on the classpath. Then add the monitorInterval="30" attribute to the opening configuration tag. This tells log4j to check the file for changes every 30 seconds.

What is the log4j command?

Log4j is an open source project based on the work of many authors. It allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files.

What is the default logging level in log4j?

Note that by default Log4j assigns the root logger to Level. ERROR.


1 Answers

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

Please note that as of log4j2, the new system variable to use is log4j.configurationFile and you put in the actual path to the file (i.e. without the file: prefix) and it will automatically load the factory based on the extension of the configuration file:

-Dlog4j.configurationFile=/path/to/log4jconfig.{ext} 
like image 126
Anubis05 Avatar answered Oct 06 '22 09:10

Anubis05