log4j 适用于 Maven Tomcat7:RUN,但不适用于 Tomcat7:Deploy


当我使用 mvn tomcat7:run log4j 时,可以

完美地工作,但是当我使用 mvn tomcat7:deploy 在本地机器上的 tomcat 上运行时,我得到了 log4j.properties 文件的 filenotfoundexception。任何想法如何解决这个问题?

 <build>
    <plugins>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <!--  --> <version>2.1</version> 
        <configuration>
            <url>http://localhost:8080/manager/text</url>
            <server>localhost</server>

        </configuration>
    </plugin>
</plugins>

更新

在log4j.properties中,我使用文件追加器记录到一个名为"loging.log"的文件。此文件是在 tomcat/bin 目录中创建的,但它是空的。

从这里找到答案

不得不从

PropertyConfigurator.configure("log4j.properties");

对此

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
PropertyConfigurator.configure(classLoader.getResource("log4j.properties"));

另一种解决方案是对 log4j.properties 文件的路径进行硬编码,但我不建议这样做:

PropertyConfigurator.configure("C:/User/...../log4j.properties");

您的 log4j.properties 文件需要位于 src/test/resources 或 src/main/resources 中

相关内容

最新更新