log4j configuration via JVM argument(s)?

2019-01-04 16:40发布

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.

7条回答
\"骚年 ilove
2楼-- · 2019-01-04 17:12

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.

查看更多
够拽才男人
3楼-- · 2019-01-04 17:14

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

查看更多
太酷不给撩
4楼-- · 2019-01-04 17:16

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
查看更多
ら.Afraid
5楼-- · 2019-01-04 17:17

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.

查看更多
戒情不戒烟
6楼-- · 2019-01-04 17:29

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:

查看更多
在下西门庆
7楼-- · 2019-01-04 17:33

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

查看更多
登录 后发表回答