Spring Boot整合SLF4j+logback

04/23 08:07
阅读数 651

一 点睛

SLF4j作为日志门面,logback作为日志实现。

Spring Boot 默认实现的日志管理就是使用的logback。

二 pom

不用修改任何地方。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.cakin</groupId>
   <artifactId>logback</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>logback</name>
   <description>logback Demo</description>


   <properties>
      <java.version>1.8</java.version>
   </properties>


   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>


      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
            <exclusion>
               <groupId>org.junit.vintage</groupId>
               <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
   </dependencies>


   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>


</project>

三 配置文件

1 路径

src/main/resources/logback-spring.xml

2 内容

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


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


    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!--要拦截的日志级别-->
            <level>ERROR</level>
            <!--如果匹配,则禁止-->
            <onMatch>DENY</onMatch>
            <!--如果不匹配,则允许记录-->
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>%d -- %msg%n</pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>e:/info-%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>


    <!--错误日志打印的文件-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--添加 范围 过滤-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <!--日志格式-->
        <encoder>
            <pattern>%d -- %msg%n</pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>e:/error-%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>


    <!--修改打印级别-->
    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>

四 测试

1 main文件测试

package com.cakin.logback;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class LogbackApplication {


    public static void main( String[] args ) {
        SpringApplication.run(LogbackApplication.class, args);
        log.debug("sl4j.debug in main");
        log.info("sl4j.info in main");
        log.error("sl4j.error in main");
    }
}

打印内容:

2020-04-22 19:44:57.206 INFO [main]com.cakin.logback.LogbackApplication.main:14 -sl4j.info in main
2020-04-22 19:44:57.209 ERROR[main]com.cakin.logback.LogbackApplication.main:15 -sl4j.error in main

2 test文件测试

package com.cakin.logback;


import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
@Slf4j
class LogbackApplicationTests {


   @Test
   void contextLoads() {
   }

   @Test
   public void testSl4j(){
      log.debug("sl4j.debug...");
      log.info("sl4j.info");
      log.error("sl4j.error");
   }
}

打印内容:

2020-04-22 19:44:10.084 INFO [main]com.cakin.logback.LogbackApplicationTests.testSl4j:18 -sl4j.info
2020-04-22 19:44:10.084 ERROR[main]com.cakin.logback.LogbackApplicationTests.testSl4j:19 -sl4j.error

五 参考

https://blog.csdn.net/duguxiaobiao/article/details/78988409

https://blog.csdn.net/sxdtzhaoxinguo/article/details/78458659

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部