1.Logback简介
Logback是由log4j创始人设计的另一款开源日志组件,主要包括以下几个模块。
logback-core:logback-classic和logback-access的基础模块。
logback-classic:log4j的改良版本,完整实现了slf4j的API,可以方便地进行日志系统更换,如log4j。
logback-access:与Servlet容器集成的访问模块,提供通过HTTP来访问日志的功能。

2.引入依赖
新建一个SpringBoot工程,并在pom.xml文件中添加集成Logback所需要的dependency。

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-core</artifactId>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-access</artifactId>
</dependency>

3.在resources目录下新建logback.xml文件,文件内容如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--输出文件位置-->
        <File>/Users/steven/Documents/代码/springBoot/logs/debug.log</File>
        <!--编码-->
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{200}][%t] %m%n</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
        <!--日志级别-->
        <filter class = "ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <!--滚动输出策略,归档文件名字-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/Users/steven/Documents/代码/springBoot/logs/debug.log.%d{yyyy-MM-dd}</fileNamePattern>
        </rollingPolicy>
    </appender>

    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>/Users/steven/Documents/代码/springBoot/logs/info.log</File>
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{200}][%t] %m%n</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>

        <filter class = "ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/Users/steven/Documents/代码/springBoot/logs/info.log.%d{yyyy-MM-dd}</fileNamePattern>
        </rollingPolicy>
    </appender>

    <appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>/Users/steven/Documents/代码/springBoot/logs/warn.log</File>
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{200}][%t] %m%n</pattern>
        </encoder>

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>warn</level>
        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/Users/steven/Documents/代码/springBoot/logs/warn.log.%d{yyyy-MM-dd}</fileNamePattern>
        </rollingPolicy>
    </appender>

    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>/Users/steven/Documents/代码/springBoot/logs/error.log</File>
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}][%p][%c{200}][%t] %m%n</pattern>
        </encoder>

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>error</level>
        </filter>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/Users/steven/Documents/代码/springBoot/logs/error.log.%d{yyyy-MM-dd}</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--全局日志级别-->
    <!--设置某个包或者类使用哪个appender-->
    <logger name="com.leichuangkj.logback" additivity="false">
        <appender-ref ref="debug" />
        <appender-ref ref="info"/>
        <appender-ref ref="warn"/>
        <appender-ref ref="error"/>
    </logger>
    <root level="debug"></root>
</configuration>

4.添加配置
在application.properties文件中添加如下配置信息。

############################################################
#
# Logback配置
#
############################################################
logging.config=classpath:logback.xml

5.测试用例
在单元测试类LogbackApplicationTests中添加如下代码。

@SpringBootTest
class LogbackApplicationTests {
	private static final org.slf4j.Logger log = LoggerFactory.getLogger(LogbackApplicationTests.class);

	@Test
	void contextLoads() {
		log.debug("log level:debug");
		log.info("log level:info");
		log.warn("log level:warn");
		log.error("log level:error");
	}
}

右键执行LogbackApplicationTests单元测试,这时可以看到控制台显示“1 test passed”以及如下打印信息。
在这里插入图片描述
在日志目录“/Users/steven/Documents/代码/springBoot/logs”下可以看到生成如下图所示的4个.log文件。
在这里插入图片描述
各文件内容如下所示。

//debug
[00:05:23.701][DEBUG][com.leichuangkj.logback.LogbackApplicationTests][main] Running with Spring Boot v2.3.1.RELEASE, Spring v5.2.7.RELEASE
[00:05:27.347][DEBUG][com.leichuangkj.logback.LogbackApplicationTests][main] log level:debug

//info
[00:05:23.697][INFO][com.leichuangkj.logback.LogbackApplicationTests][main] Starting LogbackApplicationTests on 192.168.0.106 with PID 8864 (started by steven in /Users/steven/Documents/代码/springBoot/logback)
[00:05:23.701][INFO][com.leichuangkj.logback.LogbackApplicationTests][main] No active profile set, falling back to default profiles: default
[00:05:26.975][INFO][com.leichuangkj.logback.LogbackApplicationTests][main] Started LogbackApplicationTests in 4.135 seconds (JVM running for 7.461)
[00:05:27.347][INFO][com.leichuangkj.logback.LogbackApplicationTests][main] log level:info

//warn
[00:05:27.347][WARN][com.leichuangkj.logback.LogbackApplicationTests][main] log level:warn
[00:05:27.347][ERROR][com.leichuangkj.logback.LogbackApplicationTests][main] log level:error

//error
[00:05:27.347][ERROR][com.leichuangkj.logback.LogbackApplicationTests][main] log level:error

6.工程目录结构
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐