Java 通过 JVM 参数进行 log4j 配置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/778933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:23:18  来源:igfitidea点击:

log4j configuration via JVM argument(s)?

javalogginglog4j

提问by jconlin

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?

我必须设置/传递哪些变量作为 JVM 的参数才能使 log4j 正常运行?正确的意思是不要抱怨并打印到控制台。我可以看一个典型的例子吗?

Note: I need to avoid creating a log4j.properties file in the application.

注意:我需要避免在应用程序中创建 log4j.properties 文件。

采纳答案by Brian Agnew

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

你有 log4j 配置文件吗?只需使用引用它

-Dlog4j.configuration={path to file}

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

其中 {path to file} 应该以 file:

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

编辑:如果您使用的是 log4j2,则需要使用

-Dlog4j.configurationFile={path to file}

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

摘自答案https://stackoverflow.com/a/34001970/552525

回答by Natalia

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

通常,只要您的 log4j.properties 文件在类路径上,Log4j 就会在 JVM 启动时自动获取它。

回答by Tim Büthe

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.

您还可以使用配置类而不是属性或 xml 文件。

-Dlog4j.configuratorClass=com.foo.BarConfigurator

See http://logging.apache.org/log4j/1.2/manual.htmlfor details.

有关详细信息,请参阅http://logging.apache.org/log4j/1.2/manual.html

回答by 30thh

The solution is using of the following JVM argument:

解决方案是使用以下 JVM 参数:

-Dlog4j.configuration={path to file}

If the file is NOT in the classpath (in WEB-INF/classesin case of Tomcat) but somewhere on you disk, use file:, like

如果文件不在类路径中(在WEB-INF/classesTomcat 的情况下)而是在磁盘上的某个位置,请使用file:,例如

-Dlog4j.configuration=file:C:\Users\me\log4j.xml

More information and examples here: http://logging.apache.org/log4j/1.2/manual.html

更多信息和示例:http: //logging.apache.org/log4j/1.2/manual.html

回答by D. L.

This seems to have changed (probably with log4j2) to:

这似乎已更改(可能与 log4j2)为:

-Dlog4j.configurationFile=file:C:\Users\me\log4j.xml

See: https://logging.apache.org/log4j/2.x/manual/configuration.html

请参阅:https: //logging.apache.org/log4j/2.x/manual/configuration.html

回答by Vineet Menon

Late to the party as since 2015, Log4J 1.x has reached EOL.

自 2015 年以来晚会,Log4J 1.x 已达到EOL

Log4J 2.x onwards the JVM optionshould be -Dlog4j.configurationFile=<filename>

Log4J 2.x 之后的JVM 选项应该是-Dlog4j.configurationFile=<filename>



P.S. <filename>could be a file relative to the class path withoutthe file:as suggested in the other answers.

PS<filename>可能是相对于类路径的文件,而不file:在其他答案建议。

回答by ridox

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

回答by emanuel07

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

如果您使用的是 gradle。您可以应用“应用程序”插件并使用以下命令

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