Configuring Logging for an Embedded Tomcat from Ma

2020-02-27 07:12发布

问题:

I'm using the Tomcat7 Maven plugin:

<plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.0-beta-1</version>
            <configuration>
                    <update>true</update>
                    <contextFile>${basedir}/conf/context.xml</contextFile>
                    <tomcatUsers>${basedir}/conf/tomcat-users.xml</tomcatUsers>
            </configuration>
 </plugin> 

I run my app as follows (which runs tomcat embedded)

mvn tomcat7:run

THE ISSUE: There is no catalina.out log file?

I want to turn on logging for the Realms so I can debug something. In the ./target/tomcat/log dir there is only access_log.* no other log files.

I've tried messing with the ./target/tomcat/conf/logging.properties file to no avail.

How can I configure logging for this Tomcat?

回答1:

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>


回答2:

Try using

    <tomcatLoggingFile>log.txt</tomcatLoggingFile>

in configuration section.



回答3:

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


回答4:

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


回答5:

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>