Slf4j + Logback 日志框架
大约 2 分钟
目前(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");
}
}
输出(默认格式)
17:54:52.524 [main] DEBUG org.example.InitTest - debug
17:54:52.527 [main] INFO org.example.InitTest - info
17:54:52.527 [main] WARN org.example.InitTest - warn
17:54:52.527 [main] ERROR org.example.InitTest - error
输出(关联配置格式)
2024-05-01 18:14:19.321 [main] DEBUG org.example.InitTest 12: debug
2024-05-01 18:14:19.324 [main] INFO org.example.InitTest 13: info
2024-05-01 18:14:19.325 [main] WARN org.example.InitTest 14: warn
2024-05-01 18:14:19.325 [main] ERROR org.example.InitTest 15: 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>
日志框架配置
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="false">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>OVER | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} %line: %msg %n</pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="stdout" />
</root>
</configuration>
然后引入桥接依赖
<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");
}
}
桥接前
OVER | 2024-05-01 19:43:00.644 [main] INFO org.example.OverTest 11: ---- over start ----
2024-05-01 19:43:00 DEBUG org.example.Alpha 9:Alpha doSomething debug
2024-05-01 19:43:00 INFO org.example.Alpha 10:Alpha doSomething info
2024-05-01 19:43:00 WARN org.example.Alpha 11:Alpha doSomething warn
2024-05-01 19:43:00 ERROR org.example.Alpha 12:Alpha doSomething error
OVER | 2024-05-01 19:43:00.695 [main] INFO org.example.OverTest 14: ---- over end ----
日志格式不一致
桥接后
OVER | 2024-05-01 22:06:51.748 [main] INFO org.example.OverTest 11: ---- over start ----
OVER | 2024-05-01 22:06:51.756 [main] INFO org.example.Alpha 10: Alpha doSomething info
OVER | 2024-05-01 22:06:51.756 [main] WARN org.example.Alpha 11: Alpha doSomething warn
OVER | 2024-05-01 22:06:51.756 [main] ERROR org.example.Alpha 12: Alpha doSomething error
OVER | 2024-05-01 22:06:51.756 [main] INFO org.example.OverTest 14: ---- over end ----
日志格式一致