通过 JVM 参数进行 Log4j 配置?

发布于 2024-07-17 23:06:53 字数 143 浏览 4 评论 0原文

我必须设置/传递哪些变量作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

如梦 2024-07-24 23:06:54

根据页面(Log4j2 - 自动配置) Log4j2 需要:

-Dlog4j2.configurationFile=<文件路径>

Log4j 将检查“log4j2.configurationFile”系统属性,如果设置,将尝试使用与文件扩展名匹配的 ConfigurationFactory 加载配置。 请注意,这不限于本地文件系统上的位置,并且可能包含 URL。

摘自:Log4j2 - 自动配置

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

旧时光的容颜 2024-07-24 23:06:54

我在 weblogic 12 中使用了下面的两个命令,它适用于 log4j2.xml

-Dlog4j.configurationFile=<path to file>
-Dlog4j2.configurationFile=<path to file>

I used this two commands below with weblogic 12 and it works for log4j2.xml

-Dlog4j.configurationFile=<path to file>
-Dlog4j2.configurationFile=<path to file>
我的奇迹 2024-07-24 23:06:54

尝试添加以下 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

土豪 2024-07-24 23:06:53

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

-Dlog4j.configuration={path to file}

{path to file} 的前缀来引用它 file:

编辑: 如果您正在使用 log4j2,则需要使用

-Dlog4j.configurationFile={path to file}

Taken fromanswer https://stackoverflow.com/a/34001970/552525

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

下壹個目標 2024-07-24 23:06:53

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

-Dlog4j.configuration={path to file}

如果文件不在类路径中(对于 Tomcat,则在 WEB-INF/classes 中),而是位于磁盘上的某个位置,请使用 file:< /code>,如

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

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

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

无戏配角 2024-07-24 23:06:53

此配置似乎在 Log4j 2 中发生了变化:

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

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

This configuration seems to have changed in Log4j 2:

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

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

我也只是我 2024-07-24 23:06:53

我知道这个问题已经得到解答,但是因为您说过,这并不完全是您正在寻找的,所以我想指出以下替代方案:

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

-Dlog4j.configuratorClass=com.foo.BarConfigurator

请参阅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.

-Dlog4j.configuratorClass=com.foo.BarConfigurator

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

故事还在继续 2024-07-24 23:06:53

自 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 the file: as suggested in the other answers.

帅冕 2024-07-24 23:06:53

一般来说,只要您的 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.

我们的影子 2024-07-24 23:06:53

相对路径也可以:

java -Dlog4j.configuration=file:".\log4j.properties" -jar com.your-1.0-SNAPSHOT.jar

java -Dlog4j.configuration=file:".\log4j.xml" -jar com.your-1.0-SNAPSHOT.jar

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
通知家属抬走 2024-07-24 23:06:53

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

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

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

  applicationDefaultJvmArgs = [
             "-Dlog4j.configurationFile=your.xml",
                              ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文