3

我在我的 java webapp 应用程序中使用 slf4j 和 log4j。当我使用 maven-jetty 插件运行它时,会创建日志文件。但是当我在 tomcat 中部署它时,所有日志都附加在 catalina.out 而不是我的日志文件中。下面是我的 Log4j.xml 文件


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[test] %p [%t] %c{1}.%M(%L) | %m%n" />
        </layout>
    </appender>
    <appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="/tmp/myApp.log" />
        <param name="MaxFileSize" value="100MB" />
        <param name="MaxBackupIndex" value="50" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{HH:mm:ss:SSS} - %5p - %C{1}.%M(%L) - %m%n" />
        </layout>
    </appender>

    <logger name="net.sf.ehcache">
        <level value="ERROR" />
    </logger>

    <!-- Suppress success logging from InteractiveAuthenticationSuccessEvent -->
    <logger name="org.acegisecurity">
        <level value="ERROR" />
    </logger>

    <logger name="org.apache">
        <level value="WARN" />
    </logger>

    <logger name="org.eclipse">
        <level value="ERROR" />
    </logger>

    <logger name="java.sql">
        <level value="ERROR" />
    </logger>

    <logger name="org.hibernate">
        <level value="WARN" />
    </logger>

    <logger name="org.codehaus.mojo">
        <level value="ERROR" />
    </logger>

    <logger name="org.hibernate.SQL">
        <level value="ERROR" />
    </logger>

    <logger name="org.springframework">
        <level value="ERROR" />
    </logger>

    <!-- Suppress warnings from Commons Validator -->
    <logger name="org.apache.commons.validator.ValidatorResources">
        <level value="ERROR" />
    </logger>

    <logger name="com.myapp">
        <level value="ERROR" />
    </logger>
    <logger name="com.myapp">
        <level value="INFO" />
    </logger>
    <root>
        <level value="INFO" />
        <appender-ref ref="ROLL" />
    </root>

</log4j:configuration>

和maven依赖如下


<dependency>
<groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.2</version>
    </dependency>

4

1 回答 1

0

就我而言,项目有 2 个 SLF4J 实现,“log4j”和“logback”。在应用程序部署 catalina.out 有此消息“SLF4J:类路径包含多个 SLF4J 绑定......”。通过 pom 中的依赖排除以及在该文件创建并由日志填充之后删除了 logback。

请参阅此链接 https://www.baeldung.com/slf4j-classpath-multiple-bindings

于 2020-11-18T20:58:01.670 回答