当我使用maven时,是否必须将log4j.properties放在src / main / rescoures中?

时间:2017-06-27 11:09:08

标签: java maven tomcat logging log4j2

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

war文件将部署在使用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时是否可以这样做。谁知道并可以提供帮助?

2 个答案:

答案 0 :(得分:2)

来自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.

所以你应该能够设置&#34; log4j.configurationFile&#34;将tomcat启动到配置文件的位置时的属性。

答案 1 :(得分:0)

这是关于log4j / logback / etc

的常见问题

如果您正在使用&#39; log4j.properties&#39;。您可以指定-D(分别通过%CATALINA_BASE%\ bin或$ {CATALINA_BASE} / bin中的setenv.bat / sh文件)。

有关格式的详细信息,请参阅One book in collection

但是,对于log4j2,如果切换到XML格式,则可以使用this answer语法。通过这样做,您可以将配置放在$ {catalina.base} /conf/log4j2-included.xml中,并且嵌入式log4j2配置将包含该文件。