简体   繁体   中英

How to configure Logging for an Embedded Tomcat from Maven plugin 'tomcat7-maven-plugin'

I'm using Maven 3.0.4 with tomcat7-maven-plugin for embedded Tomcat server. I would like to generate the server log through editing pom.xml. However, I can't get any log with the "tomcatLoggingFile" property in the configuration section. Below is my configuration:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
        <tomcatLoggingFile>tomcat_server.log</tomcatLoggingFile>
    </configuration>
</plugin>

I've checked the official documentation for tomcat7-maven-plugin: http://tomcat.apache.org/maven-plugin-2.1/tomcat7-maven-plugin/run-mojo.html but still don't know why it's not working.

Also, I've checked this post: Configuring Logging for an Embedded Tomcat from Maven plugin and use org.slf4j.LoggerFactory instead. There is no any log found in the file tomcat_server.log.

When I switch back to use jboss-as-maven-plugin in pom.xml and run mvn jboss-as:run , the server.log can be generated inside target folder successfully.

Any suggestion?

The documentations says, that tomcatLoggingFile refers to "the path of the Tomcat logging configuration", not the logging file itself. Besides you shall ensure, that the logging libraries are on the classpath. See http://tomcat.apache.org/tomcat-7.0-doc/logging.html

The logging configuration for Embedded Tomcat Maven is currently broken due to bug

https://issues.apache.org/jira/browse/MTOMCAT-127

The workaround is to simply redirect the stdout, like:

mvn tomcat7:run 2>&1 | tee catalina.out

Use log4j or another logging library and here it is a tricky part.... you should add extra dependencies in your maven plugin configuration

      <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                ....
                <extraDependencies>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                        <version>1.7.2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>jul-to-slf4j</artifactId>
                        <version>1.7.2</version>
                    </dependency>
                    <dependency>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>1.2.17</version>
                    </dependency>
                </extraDependencies>
            </configuration>
        </plugin>

我刚刚在web.xml中包含的xml配置中使用了log4j的文件appender

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