SpringBoot2.X中lagback详细配置及使用

文章目录

    • 一、前言
    • 二、自定义日志配置
      • 1.约束
      • 2.自定义
    • 三、详细配置
    • 四、使用
      • 1. 类方式
      • 2.注解方式
    • 五、Mybatis log 插件
      • 1.地址
      • 2.安装
      • 3.使用

一、前言

Spring Boot默认集成了Logback,可以开箱即用,非常方便。因为spring-boot-starter-logging是Logback的日志实现,而Spring Boot启动项spring-boot-starter又依赖了spring-boot-starter-logging,所以Spring Boot就默认集成了Logback.

SpringBoot2.X中lagback详细配置及使用_第1张图片

二、自定义日志配置

1.约束

日志服务在ApplicationContext创建前就初始化了,所以通过设置属性和传统的配置XML文件,可以对日志进行管理和控制。只需要在src/main/resources下,创建好约定名称的XML文件,即可完成对日志系统的设置,不同的日志系统有不同的约定名称。

日志系统 配置文件名称
logback logback-spring.xml, logback-spring.groovy, logback.xml, 或者 logback.groovy
log4j2 log4j2-spring.xml 或者 log4j2.xml

Spring Boot官方建议使用“-spring”的命名规则,进行日志配置,如:logback-spring.xml而不是logback.xml。

2.自定义

在配置文件中指定即可

# 日志
logging:
  config: classpath:log/logback-dev.xml

三、详细配置











<configuration  scan="true" scanPeriod="10 seconds">

	
    <contextName>logbackcontextName>
    
    
    <property name="log.path" value="/home/huitian/fgap/logs" />

    
    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>


    

    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    
        
        
        
        
       
        
        <encoder>
        	  
            <Pattern>${CONSOLE_LOG_PATTERN}Pattern>
            
            <charset>UTF-8charset>
        encoder>
    appender>


    
    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.path}/log_debug.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>15maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>debuglevel>
           
          	
            <onMatch>ACCEPTonMatch>
            
            
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.path}/log_info.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset>
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>15maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>infolevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.path}/log_warn.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>15maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warnlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${log.path}/log_error.logfile>
        
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset> 
        encoder>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>100MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <maxHistory>15maxHistory>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <logger name="com.apache.ibatis" level="info"/>
    <logger name="java.sql.Connection" level="info"/>
    <logger name="java.sql.Statement" level="info"/>
    <logger name="java.sql.PreparedStatement" level="info"/>
    <logger name="org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener" level="info"/>
    <logger name="org.springframework.core.env.PropertySourcesPropertyResolver" level="info"/>
    <logger name="org.springframework.beans.factory.support.DefaultListableBeanFactory" level="info"/>

    
    
    
    <springProfile name="test,dev">
        <logger name="com.example.demo.controller" level="DEBUG" additivity="false">
            <appender-ref ref="consoleLog"/>
        logger>
  	springProfile>
  	
  	    
    <springProfile name="prod">
        <logger name="com.example.demo.controller" level="INFO" additivity="false">
           <appender-ref ref="DEBUG_FILE" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="WARN_FILE" />
            <appender-ref ref="ERROR_FILE" />
        logger>
  	springProfile>
	

configuration>

四、使用

1. 类方式

package com.dingwen.lir.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * log 测试
 *
 * @author dingwen
 * 2021.05.07 11:01
 */
public class LogController {
     
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void test() {
     
        int index = 0;
        logger.info("index={}", index);
    }
}

2.注解方式

需要安装lombok 插件

package com.dingwen.lir.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * log 测试
 *
 * @author dingwen
 * 2021.05.07 11:01
 */
@Slf4j
public class LogController {
     
    public void test() {
     
        int index = 0;
        log.info("index={}", index);
    }
}


五、Mybatis log 插件

可以直接根据参数拼接出可以直接执行的SQL语句,方便排错。

1.地址

百度云https://pan.baidu.com/s/1TXU-Jlg-CEgbiKQCa0MyQg 提取码ding

2.安装

SpringBoot2.X中lagback详细配置及使用_第2张图片

3.使用

SpringBoot2.X中lagback详细配置及使用_第3张图片

你可能感兴趣的:(SpringBoot,logback,spring,boot)