Spring:使用log4j或logback时,日志记录不工作[英] Spring : Logging not working with log4j or logback

本文是小编为大家收集整理的关于Spring:使用log4j或logback时,日志记录不工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在研究一个春季MVC应用程序,我正在尝试再次登录.不幸的是,有时候它只是停止工作,我不知道是什么原因造成的.我在网上尝试了一些建议,但没有任何帮助.有什么建议吗?

pom.xml:

<packaging>war</packaging>
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.1.6.RELEASE</org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>4.3.9.Final</hibernate.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <springsecurity.version>4.0.1.RELEASE</springsecurity.version>
        <spring-platform.version>1.1.3.RELEASE</spring-platform.version>
        <jetty.version>9.2.9.v20150224</jetty.version>
    </properties>

    <parent>
        <groupId>io.spring.platform</groupId>
        <artifactId>platform-bom</artifactId>
        <version>1.1.3.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>

  <!-- Spring framework dependencies -->

        <dependency>
            <groupId>org.springframework.mobile</groupId>
            <artifactId>spring-mobile-device</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>

        <!-- Spring security dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>


    <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.13</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>

我同时具有log4j.xml和logback.xml:

log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>


    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p: %c - %m%n" />
        </layout>
    </appender>
  <logger name="org.cometd">
        <level value="debug"/>
    </logger>
    <!-- Root Logger -->
    <root>
        <priority value="OFF" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>


    <logger name="org.cometd" level="debug"/>
    <logger name="com.tooltank.spring.chat.ChatServiceImpl" level="info"/>
</configuration>

在服务器启动期间,我得到了:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home//WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:43:29,174 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/path/to/logback.xml]
13:43:29,292 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:43:29,298 |-ERROR in ch.qos.logback.core.joran.action.IncludeAction - Could not find resource corresponding to [org/springframework/boot/logging/logback/base.xml]
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.cometd] to DEBUG
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.tooltank.spring.chat.ChatServiceImpl] to INFO
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:43:29,300 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@59474a9e - Registering current configuration as safe fallback point

我想念什么?谢谢.

推荐答案

您应该避免在一个应用程序中使用log4j和logback.如果您的课堂路径中都有两个罐子,则classloader会选择其中一个(一种随机的.) 这是由此日志语句指示的:

SLF4J: Class path contains multiple SLF4J bindings.

如果您打算使用logback,则需要将logback.xml文件放在应用程序的类路径中.从文档:

logBack将尝试使用文件logback-test.xml或logback.xml进行配置,如果在类路径上找到.

您还可以查看以下链接: https://docs.spring.io/spring-boot/docs/current/current/referent/referent/html/howto-logging.html#howto-howto-howto-configure-configure-configure-logback-for-for-for-for-for-logging

编辑: 我想您还需要在控制台上添加一个附录器,例如:

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
     ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
  <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>

,然后将Appender添加到您的记录器中:

<logger name="org.cometd" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="STDOUT" />
</logger>

最好的问候!

其他推荐答案

我也有同样的问题. 我将两个<groupId>org.slf4j</groupId>&<groupId>ch.qos.logback</groupId>依赖项混合在一起. 删除<groupId>org.slf4j</groupId>并仅保留<groupId>ch.qos.logback</groupId>日志后.

本文地址:https://www.itbaoku.cn/post/1575084.html

问题描述

I am working on a Spring-MVC application in which I am trying to get logging working again. Unfortunately, sometime back it just stopped working, I don't know what is causing that. I tried some suggestions on net, but nothing useful. Any suggestions?

Pom.xml :

<packaging>war</packaging>
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.1.6.RELEASE</org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>4.3.9.Final</hibernate.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <springsecurity.version>4.0.1.RELEASE</springsecurity.version>
        <spring-platform.version>1.1.3.RELEASE</spring-platform.version>
        <jetty.version>9.2.9.v20150224</jetty.version>
    </properties>

    <parent>
        <groupId>io.spring.platform</groupId>
        <artifactId>platform-bom</artifactId>
        <version>1.1.3.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>

  <!-- Spring framework dependencies -->

        <dependency>
            <groupId>org.springframework.mobile</groupId>
            <artifactId>spring-mobile-device</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>

        <!-- Spring security dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>


    <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.13</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>

I have both log4j.xml and logback.xml :

log4j.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>


    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p: %c - %m%n" />
        </layout>
    </appender>
  <logger name="org.cometd">
        <level value="debug"/>
    </logger>
    <!-- Root Logger -->
    <root>
        <priority value="OFF" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

logback.xml :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>


    <logger name="org.cometd" level="debug"/>
    <logger name="com.tooltank.spring.chat.ChatServiceImpl" level="info"/>
</configuration>

During server startup I get this :

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home//WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:43:29,174 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/path/to/logback.xml]
13:43:29,292 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:43:29,298 |-ERROR in ch.qos.logback.core.joran.action.IncludeAction - Could not find resource corresponding to [org/springframework/boot/logging/logback/base.xml]
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.cometd] to DEBUG
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.tooltank.spring.chat.ChatServiceImpl] to INFO
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:43:29,300 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@59474a9e - Registering current configuration as safe fallback point

What am I missing? Thank you.

推荐答案

You should avoid using log4j and logback in one application. If you have both jars in your classpath the classloader will pick either one of them (kind of random..) That is indicated by this log statement:

SLF4J: Class path contains multiple SLF4J bindings.

If you intend to use logback you need to place the logback.xml file in the classpath of your application. From documentation:

logback will try to configure itself using the files logback-test.xml or logback.xml if found on the class path.

You can also check out this link: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-logback-for-logging

Edit: I guess you also need to add an appender to the console such as:

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
     ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
  <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>

and then add the appender to your logger:

<logger name="org.cometd" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="STDOUT" />
</logger>

Best regards!

其他推荐答案

I had the same issue. I had mixed both <groupId>org.slf4j</groupId> & <groupId>ch.qos.logback</groupId> dependencies together. After I remove <groupId>org.slf4j</groupId> and keep only <groupId>ch.qos.logback</groupId> logs are started to written.