跳至主要內容

Slf4j + Logback 日志框架

Steven大约 2 分钟java

目前(2024 年 5 月 2 日)主流日志框架之一。

例子:Slf4j 适配 Logback

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>demo-java-log</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>demo-usage-slf4j-logback</artifactId>

    <dependencies>
<!--        <dependency>-->
<!--            <groupId>org.slf4j</groupId>-->
<!--            <artifactId>slf4j-api</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>ch.qos.logback</groupId>-->
<!--            <artifactId>logback-core</artifactId>-->
<!--        </dependency>-->
        <!-- 默认依赖 slf4j-api 和 logback-core -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
    </dependencies>
</project>

配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="false">
    <property name="LOG_HOME" value="target/log-demo/logback/" />
    <property name="appName" value="spider" />

    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} %line: %msg %n</pattern>
        </layout>
    </appender>

    <appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/${appName}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
            <maxHistory>8</maxHistory>
            <maxFileSize>100MB</maxFileSize>
            <totalSizeCap>1GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} %line: %msg %n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- additivity="false" 表示父类日志器不重复输出 -->
    <logger name="org.example" level="debug" additivity="false">
        <appender-ref ref="stdout" />
        <appender-ref ref="appLogAppender" />
    </logger>

    <root level="info">
        <appender-ref ref="stdout" />
    </root>
</configuration>

相关信息

logback 不像 log4j 那样必须要有配置文件,它配置了默认的输出方式。

使用例子

测试代码
package org.example;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InitTest {
    private static final Logger logger = LoggerFactory.getLogger(InitTest.class);
    @Test
    void testInit() {
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
    }
}

异步日志

<appender class="ch.qos.logback.classic.AsyncAppender" >
    <appender-ref ref="stdout" />
</appender>

例子:Slf4j 桥接 Log4j 到 Logback

统一日志输出框架、日志输出格式

首先按上面步骤配置好 logback

依赖引入
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>

然后引入桥接依赖

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
</dependency>

然后排除三方件中的原日志框架依赖或者将桥接依赖放在三方依赖前 - maven 优先级)

<!-- 三方件 -->
<dependency>
    <groupId>org.example</groupId>
    <artifactId>demo-usage-log4j-basic</artifactId>
    <version>1.0-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

使用例子

测试代码
package org.example;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InitTest {
    private static final Logger logger = LoggerFactory.getLogger(InitTest.class);
    @Test
    void testInit() {
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
    }
}