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.
Based on @lijat, here is a simplified implementation. In my spring-based application I simply load this as a bean.
Based on Thorbjørn Ravn Andersens suggestion I wrote some code that makes this work
Add the following early in the main method and it is now possible to set the log level from the comand line. This have been tested in a project of mine but I'm new to log4j and might have made some mistake. If so please correct me.
log4j does not support this directly.
As you do not want a configuration file, you most likely use programmatic configuration. I would suggest that you look into scanning all the system properties, and explicitly program what you want based on this.
In my pretty standard setup I've been seeing the following work well when passed in as VM Option (commandline before class in Java, or VM Option in an IDE):
With Log4j2, this can be achieved using the following utility method added to your code.
You need these imports
Now invoke the
setLogLevel
method in your main() or whereever appropriate and pass command line params-Dlog4j.logger=com.mypackage.Thingie
and-Dlog4j.debug=true
.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 thefile:
prefix) and it will automatically load the factory based on the extension of the configuration file: