2017-06-27 24 views
0

我有一个部署在tomcat上的web-API。我想使用log4j2从API打印,以便我可以跟踪对API进行的调用。当我使用maven时,是否必须将log4j.properties放置在src/main/rescoures中?

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

正如我所了解的,当使用maven(我正在使用maven)时,log4j.properties文件应该放在src/main/rescources中。当我把文件放在那个文件夹中时,我得到了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时这是否可行。任何人知道并可以提供帮助?

+0

该文件被打包到您正在使用的war文件中,那么该文件如何被覆盖?为什么你要更改资源配置?保持约定.... – khmarbaise

+0

你是什么意思 - log4j.properties文件将被覆盖? –

+0

https://stackoverflow.com/questions/7390521/change-location-of-log4j-properties – Stefan

回答

1

从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”属性。

+0

使用该参考数据的好信息! –

0

这是关于log4j的一个共同的问题/的logback /等

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

请参阅this answer了解格式的更多详细信息。

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

+0

如何使用log4j2作为xml?我试图将它用作XML格式而不是log4j.properties。但是,我没有得到任何日志只有以下错误: log4j:警告没有appender可以找到记录器(com.cgi.searcheditor.jerseyjackson.jaxrs.resource.MyClass)。 log4j:WARN请正确初始化log4j系统。 log4j:WARN请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig了解更多信息。 – Pernilladwl

相关问题