【问题标题】:Configuring Logging for an Embedded Tomcat from Maven plugin从 Maven 插件为嵌入式 Tomcat 配置日志记录
【发布时间】:2012-05-30 10:35:27
【问题描述】:

我正在使用 Tomcat7 Maven 插件:

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

我按如下方式运行我的应用程序(运行嵌入的 tomcat)

mvn tomcat7:run

问题:没有 catalina.out 日志文件?

我想打开领域的日志记录,这样我就可以调试一些东西了。在 ./target/tomcat/log 目录中只有 access_log。* 没有其他日志文件。

我尝试弄乱 ./target/tomcat/conf/logging.properties 文件无济于事。

如何为此 Tomcat 配置日志记录?

【问题讨论】:

  • 好问题。我相信有一个真正的错误会阻止创建日志。对我来说,我在tomcat/logs 文件夹中获得的唯一日志文件是access.log。我没有得到其他日志,例如catalina.out日志等。

标签: tomcat logging configuration maven-plugin


【解决方案1】:

我找到了解决方案,您需要描述日志库的额外依赖项。就我而言,它是logback,如果您使用 log4j,只需更改依赖项。有用... 在我的配置下方:

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

    Embedded Tomcat Maven 的日志记录配置目前因错误而中断

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

    解决方法是简单地重定向标准输出,例如:

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

    【讨论】:

      【解决方案3】:

      我的解决方案是,

                  String logBackfile  ="....."; //the logback config
                  LoggerContext lc = new LoggerContext();
      
                  JoranConfigurator configurator = new JoranConfigurator();  
                  configurator.setContext(lc);  
                  lc.reset();  
                  configurator.doConfigure(logBackfile);
                  StatusPrinter.printInCaseOfErrorsOrWarnings(lc);  
      

      【讨论】:

        【解决方案4】:

        这只是部分答案,但我让它像这样工作,我的应用程序包含自己的 logback 依赖项(无需声明 extraDependencies)。

        这里唯一需要注意的是,当我的应用程序中出现较低级别的错误时(在应用程序加载和/或其他之前),我仍然无法获得所需的 Tomcat catalina.log 输出。使用这种配置,我只能得到我的应用程序级别的日志文件(不是我真正想要的 logs/catalina.out):

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

        【讨论】:

          【解决方案5】:

          尝试使用

              <tomcatLoggingFile>log.txt</tomcatLoggingFile>
          

          在配置部分。

          【讨论】:

          • 这是您指定备用 logging.properties 而不是要生成的日志文件的名称
          猜你喜欢
          • 2013-06-03
          • 2011-11-02
          • 2014-02-23
          • 1970-01-01
          • 2012-03-25
          • 2012-06-24
          • 2018-01-07
          • 2011-02-24
          • 1970-01-01
          相关资源
          最近更新 更多