'log4j configuration via JVM argument(s)?
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.
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
Generally, as long as your log4j.properties file is on the classpath, Log4j should just automatically pick it up at JVM startup.
Solution 6:[6]
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
Solution 7:[7]
If you are using gradle. You can apply 'aplication' plugin and use the following command
applicationDefaultJvmArgs = [
"-Dlog4j.configurationFile=your.xml",
]
Solution 8:[8]
According to the page (Log4j2 - Automatic Configuration) Log4j2 needs:
-Dlog4j2.configurationFile=<path to file>
Log4j will inspect the "log4j2.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.
Taken from: Log4j2 - Automatic Configuration
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 30thh |
| Solution 2 | D. L. |
| Solution 3 | Tim Büthe |
| Solution 4 | Vineet Menon |
| Solution 5 | Natalia |
| Solution 6 | ridox |
| Solution 7 | emanuel07 |
| Solution 8 | Seweryn Habdank-Wojewódzki |
