What variables do I have to set/pass as arguments to the JVM to get log4j to run properly? And by properly I mean not complain and print to the console. Can I see a typical example?
Note: I need to avoid creating a log4j.properties file in the application.
This seems to have changed (probably with log4j2) to:
-Dlog4j.configurationFile=file:C:\Users\me\log4j.xml
See: https://logging.apache.org/log4j/2.x/manual/configuration.html
Generally, as long as your log4j.properties file is on the classpath, Log4j should just automatically pick it up at JVM startup.
If you are using gradle. You can apply 'aplication' plugin and use the following command
applicationDefaultJvmArgs = [
"-Dlog4j.configurationFile=your.xml",
]
I know this is already answered, but because you said, this isn't exactly what you are looking for, I would like to point out the following alternative:
You can also use a configuration class instead of the properties or xml file.
-Dlog4j.configuratorClass=com.foo.BarConfigurator
See http://logging.apache.org/log4j/1.2/manual.html for details.
Late to the party as since 2015, Log4J 1.x has reached EOL.
Log4J 2.x onwards the JVM option should be -Dlog4j.configurationFile=<filename>
P.S. <filename>
could be a file relative to the class path without the file:
as suggested in the other answers.
The solution is using of the following JVM argument:
-Dlog4j.configuration={path to file}
If the file is NOT in the classpath (in WEB-INF/classes
in case of Tomcat) but somewhere on you disk, use file:
, like
-Dlog4j.configuration=file:C:\Users\me\log4j.xml
More information and examples here: http://logging.apache.org/log4j/1.2/manual.html
Relative Path is also ok:
java -Dlog4j.configuration=file:".\log4j.properties" -jar com.your-1.0-SNAPSHOT.jar
or
java -Dlog4j.configuration=file:".\log4j.xml" -jar com.your-1.0-SNAPSHOT.jar
Source: Stackoverflow.com