SpringBoot+Slf4j+Logback实现分包打印记录日志

SpringBoot+Slf4j+Logback实现分包打印记录日志

系列文章目录

Slf4j+Logback实现分包分路径打印记录Log日志


文章目录

    • SpringBoot+Slf4j+Logback实现分包打印记录日志
  • 系列文章目录
  • 前言
  • 一、Slf4j和Logback是什么?
  • 二、使用步骤
    • 1.引入Maven依赖
    • 2.配置logback-spring.xml
  • 总结


前言

随着项目代码增加,业务增多,把所有日志Log日志记录到一个文件里面,这样排查问题也挺苦难的


提示:以下是本篇文章正文内容,下面案例可供参考

一、Slf4j和Logback是什么?

Slf4j类似于Commons Logging,也是一个日志接口,而Logback类似于Log4j,是一个日志的实现。
Logback是由log4j创始人设计的另一个开源日志组件

二、使用步骤

1.引入Maven依赖

代码如下(示例):

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

2.配置logback-spring.xml

代码如下(示例):


<configuration>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            pattern>
        layout>
    appender>

    
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/info/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/error/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    <appender name="xxxxxInfo"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
        rollingPolicy>
    appender>

    
    <logger name="com.xxxxx.service.log.xxxxx.LogAppInfo" level="info" additivity="false">
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
    logger>

    <appender name="xxxxxInfo"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
        rollingPolicy>
    appender>

    
    <logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
    logger>

    <appender name="xxxxxInfo"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
        rollingPolicy>
    appender>

    
    <logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
    logger>

    <appender name="xxxxxInfo"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
    logger>

    <appender name="xxxxxInfo"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <append>trueappend>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/info/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <appender name="xxxxxError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERRORlevel>
        filter>
        <encoder>
            <pattern>
                %d - %msg%n
            pattern>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>xxxxx/xxxxx/error/%d.logfileNamePattern>
        rollingPolicy>
    appender>
    
    <logger name="com.xxxxx.service.log.xxxxx.xxxxxInfo" level="info" additivity="false">
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
    logger>

    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
        <appender-ref ref="xxxxxError"/>
        <appender-ref ref="xxxxxError"/>
        <appender-ref ref="xxxxxError"/>
        <appender-ref ref="xxxxxInfo"/>
        <appender-ref ref="xxxxxError"/>
    root>
configuration>

以上是配置XML


总结

直接复制下来可以使用

你可能感兴趣的:(Spring,Boot,logback,slf4j,java,spring)