Configuring Logging for an Embedded Tomcat from Maven plugin

不羁的心 提交于 2019-11-30 11:09:54

I found solution, you need describe extra dependencies of your logging library. In my case its logback, if you use log4j just change dependencies. It works... below my config:

       <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <path>/myapp</path>
                <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>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                        <version>1.0.7</version>
                    </dependency>
                    <dependency>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-core</artifactId>
                        <version>1.0.7</version>
                    </dependency>
                </extraDependencies>
            </configuration>
        </plugin>

Try using

    <tomcatLoggingFile>log.txt</tomcatLoggingFile>

in configuration section.

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

My solution is,

            String logBackfile  ="....."; //the logback config
            LoggerContext lc = new LoggerContext();

            JoranConfigurator configurator = new JoranConfigurator();  
            configurator.setContext(lc);  
            lc.reset();  
            configurator.doConfigure(logBackfile);
            StatusPrinter.printInCaseOfErrorsOrWarnings(lc);  

This is only a partial answer, but I got it working like this, where my app contains its own logback dependencies (no need to declare extraDependencies).

The only caveat here is that I still am not able to get the Tomcat catalina.log output that I need when there is a lower level error in my application (before the app loads and/or other). With this configuration, I only get my application level log file (not the logs/catalina.out that I really want):

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version><!-- Tomcat 7.0.47 -->
    <configuration>
        <port>9090</port>
        <path>/${project.artifactId}</path>
        <systemProperties>
            <spring.profiles.active>webService</spring.profiles.active>
            <java.util.logging.config.file>src/integration-test/resources/logback.xml</java.util.logging.config.file>
        </systemProperties>
    </configuration>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>tomcat-shutdown</id>
            <goals>
                <goal>shutdown</goal>
            </goals>
            <phase>post-integration-test</phase>
        </execution>
    </executions>
</plugin>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!