gpt4 book ai didi

java - 使用 maven 时是否必须将 log4j.properties 放在 src/main/resources 中?

转载 作者:行者123 更新时间:2023-11-28 22:40:02 28 4
gpt4 key购买 nike

我有一个部署在 tomcat 上的网络 API。我想使用 log4j2 从 API 打印,这样我就可以跟踪对 API 的调用。

war-file 会部署在不同机器上的tomcat。我希望能够将 log4j.properties 文件放置在我每次进行新部署时都不会覆盖它的地方。

据我所知,在使用 maven 时,log4j.properties 文件应该放在 src/main/rescources 中(我正在使用 maven)。当我将文件放在该文件夹中时,log4j2 开始工作。但是,如果我进行新部署,log4j.properties 文件将被覆盖。

当我使用 Maven 时,是否可以将 log4j.propterties 文件放在该文件夹之外?也许在 tomcat 文件夹中?

我已尝试对 pom.xml 进行更改。我试图改变两个地方的位置。

<log4j.configuration>./conf/log4j.properties</log4j.configuration>

    <resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>

我尝试一次只更改其中一个的位置,然后同时更改两个的位置。我尝试将 log4j.properties 放在 tomcat 目录中。

我不知道当您使用 maven 时是否可以这样做。任何知道并可以提供帮助的人?

最佳答案

来自 Log4j 2 文档:

Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

因此,当您启动 tomcat 时,您应该能够将“log4j.configurationFile”属性设置到配置文件的位置。

关于java - 使用 maven 时是否必须将 log4j.properties 放在 src/main/resources 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44778785/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com