简体   繁体   中英

log4j works with maven tomcat7:run but not with tomcat7:deploy

When I use mvn tomcat7:run log4j works perfectly but when I use mvn tomcat7:deploy to run in on the tomcat on my local machine I get filenotfoundexception for the log4j.properties files. Any ideas how can I fix this?

 <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>

UPDATE

In log4j.properties I am using file appender to log to a file called "loging.log". This file is creatd in the tomcat/bin directory but it is empty.

Found answer from here

Had to change from

PropertyConfigurator.configure("log4j.properties");

to this

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

Another solution is to hard code the path of the log4j.properties file but I dont recommend it:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM