log4j configuration via JVM argument(s)?

JavaLoggingLog4j

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

Do you have a log4j configuration file ? Just reference it using

-Dlog4j.configuration={path to file}

where {path to file} should be prefixed with file:

Edit: If you are working with log4j2, you need to use

-Dlog4j.configurationFile={path to file}

Taken from answer https://stackoverflow.com/a/34001970/552525

Solution 2 - Java

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 3 - Java

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 4 - Java

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 5 - Java

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 6 - Java

Generally, as long as your log4j.properties file is on the classpath, Log4j should just automatically pick it up at JVM startup.

Solution 7 - Java

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 8 - Java

If you are using gradle. You can apply 'aplication' plugin and use the following command

  applicationDefaultJvmArgs = [
             "-Dlog4j.configurationFile=your.xml",
                              ]

Solution 9 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjconlinView Question on Stackoverflow
Solution 1 - JavaBrian AgnewView Answer on Stackoverflow
Solution 2 - Java30thhView Answer on Stackoverflow
Solution 3 - JavaD. L.View Answer on Stackoverflow
Solution 4 - JavaTim BütheView Answer on Stackoverflow
Solution 5 - JavaVineet MenonView Answer on Stackoverflow
Solution 6 - JavaNataliaView Answer on Stackoverflow
Solution 7 - JavaridoxView Answer on Stackoverflow
Solution 8 - Javaemanuel07View Answer on Stackoverflow
Solution 9 - JavaSeweryn Habdank-WojewódzkiView Answer on Stackoverflow