java - 没有 jcl-over-slf4j 的 Spring + Logback(slf4j) 日志记录

标签 java spring maven logging logback

如果我通过像这样添加 Maven 依赖项将 Logback 和 Slf4j 添加到我的 Spring MVC(版本 4.1.0)项目中:

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.21</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.21</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.7</version>
                <scope>runtime</scope>
            </dependency>

并排除 commons-logging 依赖项,然后我进入日志文件,除了应用程序日志之外,还有一些 spring 事件信息级别,例如:

[RMI TCP Connection(4)-127.0.0.1][org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started

如果我在没有 jcl-over-slf4j 的情况下添加依赖项,并且没有在 pom.xml 中排除 commons-logging,那么我只会获得应用程序日志。

这是我的 logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="LOG_HOME" value="logs" />
    <property name="APP_NAME" value="App" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">        
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} %thread %logger{50} - %msg%n</pattern>            
        </encoder>
    </appender>

    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/${APP_NAME}.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>

        <encoder>
            <charset>UTF-8</charset>
            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} %thread %logger{50} - %msg%n</pattern>                
        </encoder>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>  

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

    <logger name="org.springframework" level="INFO" appender-ref="FILE"/>
</configuration>

据我了解,发生这种情况是因为当我不使用 JCL 桥时,logback 无法捕获或翻译 JCL spring 内部日志。如果我错了,请纠正我。

我可以配置 logback.xml 以在没有 JCL 桥的情况下获取标准的 spring 日志事件吗?

最佳答案

检查他们的 docs ,因为它是包含大量示例的良好信息来源。 回到你的问题: Spring 和 JCL:

The mandatory logging dependency in Spring is the Jakarta Commons Logging API (JCL). We compile against JCL and we also make JCL Log objects visible for classes that extend the Spring Framework. It’s important to users that all versions of Spring use the same logging library: migration is easy because backwards compatibility is preserved even with applications that extend Spring.The way we do this is to make one of the modules in Spring depend explicitly on commons-logging (the canonical implementation of JCL), and then make all the other modules depend on that at compile time. If you are using Maven for example, and wondering where you picked up the dependency on commons-logging, then it is from Spring and specifically from the central module calledspring-core.

Logback 配置,这里是重点:

A more common choice amongst SLF4J users, which uses fewer step sand generates fewer dependencies, is to bind directly to Logback. This removes the extra binding step because Logback implements SLF4J directly, so you only need to depend on two libaries not four (jcl-over-slf4j and logback). If you do that you might also need to exclude the slf4j-api dependency from other external dependencies (not Spring), because you only want one version of that API on the classpath.

关于java - 没有 jcl-over-slf4j 的 Spring + Logback(slf4j) 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38398366/

相关文章:

java - 将 TimeUnit 转换为 ChronoUnit?

spring - 主模块和测试模块的单个 application.properties

maven - brew 安装Maven 404错误

maven - 在Windows 7 32位计算机上安装Hadoop 2.7.1时出错

maven - 执行 javac : Compilation failure 失败

java - Executors.newSingleThreadScheduledExecutor 的用法

java - 数据加载仅来自 onStart 而不是 Fragment 中的 OnActivityCreated

java - 录音机未处理的异常类型 IOException

java - Spring和Soap客户端,使用池化吗?

java - 如何在 Spring 5 中使用自定义 'TransactionAttributeSource'