通过 JVM 参数进行 Log4j 配置?
我必须设置/传递哪些变量作为 JVM 的参数才能使 Log4j 正常运行? 我所说的正确是指不要抱怨并打印到控制台。 我可以看一个典型的例子吗?
注意:我需要避免在应用程序中创建 log4j.properties
文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
根据页面(Log4j2 - 自动配置) Log4j2 需要:
-Dlog4j2.configurationFile=<文件路径>
摘自:Log4j2 - 自动配置
According to the page (Log4j2 - Automatic Configuration) Log4j2 needs:
-Dlog4j2.configurationFile=<path to file>
Taken from: Log4j2 - Automatic Configuration
我在 weblogic 12 中使用了下面的两个命令,它适用于 log4j2.xml
I used this two commands below with weblogic 12 and it works for log4j2.xml
尝试添加以下 JVM 参数:
-Dorg.apache.logging.log4j.level=INFO
来源:
https://medium.com/@kasunbg/更改-log4j2-default-logging-level-to-info-b25db8723eb9
Try adding the following JVM argument:
-Dorg.apache.logging.log4j.level=INFO
Source:
https://medium.com/@kasunbg/change-log4j2-default-logging-level-to-info-b25db8723eb9
你有 log4j 配置文件吗? 只需使用
{path to file} 的前缀来引用它
file:
编辑: 如果您正在使用 log4j2,则需要使用
Taken fromanswer https://stackoverflow.com/a/34001970/552525
Do you have a log4j configuration file ? Just reference it using
where {path to file} should be prefixed with
file:
Edit: If you are working with log4j2, you need to use
Taken from answer https://stackoverflow.com/a/34001970/552525
解决方案是使用以下 JVM 参数:
如果文件不在类路径中(对于 Tomcat,则在
WEB-INF/classes
中),而是位于磁盘上的某个位置,请使用file:< /code>,如
更多信息和示例:http://logging.apache.org/ log4j/1.2/manual.html
The solution is using of the following JVM argument:
If the file is NOT in the classpath (in
WEB-INF/classes
in case of Tomcat) but somewhere on you disk, usefile:
, likeMore information and examples here: http://logging.apache.org/log4j/1.2/manual.html
此配置似乎在 Log4j 2 中发生了变化:
请参阅:https://logging.apache.org/log4j/2.x/manual/configuration.html
This configuration seems to have changed in Log4j 2:
See: https://logging.apache.org/log4j/2.x/manual/configuration.html
我知道这个问题已经得到解答,但是因为您说过,这并不完全是您正在寻找的,所以我想指出以下替代方案:
您还可以使用配置类而不是属性或 xml 文件。
请参阅http://logging.apache.org/log4j/1.2/manual.html 了解详情。
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.
See http://logging.apache.org/log4j/1.2/manual.html for details.
自 2015 年以来,Log4J 1.x 已经到了 EOL。
Log4J 2.x 及以上 JVM 选项 应该是 < code>-Dlog4j.configurationFile=
PS
可以是相对于类路径的文件不带文件:
按照其他答案中的建议。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 thefile:
as suggested in the other answers.一般来说,只要您的 log4j.properties 文件位于类路径中,Log4j 就应该在 JVM 启动时自动选取它。
Generally, as long as your log4j.properties file is on the classpath, Log4j should just automatically pick it up at JVM startup.
相对路径也可以:
或
Relative Path is also ok:
or
如果你使用的是gradle。 您可以应用“应用程序”插件并使用以下命令
If you are using gradle. You can apply 'aplication' plugin and use the following command