【LogBack日志】打印mybatis中sql日志并存放到指定文件中

前言:

在springboot中使用logbac-spring.xml单独打印mybaits中的sql,另存一个单独的日志中。

1、yml配置

# 日志配置
logging:
  level:
    com.xxxx.mapper: debug
    org.springframework: warn
  config: classpath:logback-app.xml
  file:
    path: ./logs/app

2、logback-spring.xml


<configuration>
    <property resource="application.yml"/>
    
    <springProperty scope="context" name="FILE_PATH" source="logging.file.path"/>
    
    
    <property name="FILE_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:  %msg%n"/>
    
    <property name="MAX_HISTORY" value="10"/>
    
    <property name="FILE_SIZE" value="10MB"/>

    
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${FILE_PATH}/sys-info.logfile>
        <append>trueappend>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${FILE_PATH}/sys-info.%d{yyyy-MM-dd}.logfileNamePattern>
            
            <MaxHistory>${MAX_HISTORY}MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${FILE_PATTERN}pattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>${FILE_SIZE}MaxFileSize>
        triggeringPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>INFOlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${FILE_PATH}/sys-error.logfile>
        <append>trueappend>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${FILE_PATH}/sys-error.%d{yyyy-MM-dd}.logfileNamePattern>
            
            <MaxHistory>${MAX_HISTORY}MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${FILE_PATTERN}pattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>${FILE_SIZE}MaxFileSize>
        triggeringPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${FILE_PATH}/sys-debug.logfile>
        <append>trueappend>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${FILE_PATH}/sys-debug.%d{yyyy-MM-dd}.logfileNamePattern>
            
            <MaxHistory>${MAX_HISTORY}MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${FILE_PATTERN}pattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>${FILE_SIZE}MaxFileSize>
        triggeringPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>DEBUGlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>>
        filter>
    appender>

    
    <appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${FILE_PATH}/sys-sql.logfile>
        <append>trueappend>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${FILE_PATH}/sys-sql.%d{yyyy-MM-dd}.logfileNamePattern>
            
            <MaxHistory>${MAX_HISTORY}MaxHistory>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${FILE_PATTERN}pattern>
        encoder>
        
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>${FILE_SIZE}MaxFileSize>
        triggeringPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>DEBUGlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>>
        filter>
    appender>

    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${FILE_PATTERN}pattern>
        encoder>
    appender>

    
    <logger name="org.springframework" level="WARN"/>

    
    <logger name="com.xxx.mapper" level="DEBUG" additivity="false">
        <appender-ref ref="SQL_FILE" />
        <appender-ref ref="STDOUT" />
    logger>

    
    <root level="INFO">
        <appender-ref ref="INFO_FILE" />
        <appender-ref ref="ERROR_FILE" />
        <appender-ref ref="DEBUG_FILE" />
        <appender-ref ref="STDOUT" />
    root>

configuration>

3、关键几点

几个关键点

a. 文件名需要为​​logback-spring.xml​​; 动态日志路径才会生效; 属性文件中配置 ​​log.path=xxx​​;

【Log日志】logback.xml动态配置属性值(包括接入的第三方配置)

b. levle 需要是DEBUG等级; 因为sql日志是DEBUG等级的;name= 存放mapper文件的包路径


    <logger name="com.xxx.mapper" level="DEBUG" additivity="false">
        <appender-ref ref="SQL_FILE" />
        <appender-ref ref="STDOUT" />
    logger>

c. mybatis的log-impl需要配置正确的实现类 比如 在maybatis-plus中

# MyBatis配置
mybatis:
  # 搜索指定包别名
  typeAliasesPackage: com.xxx.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  configuration:
    # 指定 MyBatis 所用日志的具体实现
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
    # 开启自动下划线格式转驼峰格式
    map-underscore-to-camel-case: true
    # 全局映射器启用缓存
    cache-enabled: true
    # 允许 JDBC 支持自动生成主键
    use-generated-keys: true
    # 执行模式。默认是 SIMPLE
    default-executor-type: reuse
    # 允许查询结果为null
    call-setters-on-nulls: true

d. 我之前就是一直配置的是 ​​log-impl: org.apache.ibatis.logging.stdout.StdOutImpl​​ ;导致文件只能出现在控制台;却没有打印到文件中;

参数值的默认值设置
如果配置文件没有设置属性. 可以在使用的地方设置默认值 例如​​​${log.path:-默认值}​

参考资料:
https://blog.51cto.com/szzdzhp/5300674
https://blog.csdn.net/happyxin_/article/details/128844723?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-128844723-blog-116700232.235%5Ev28%5Epc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-2-128844723-blog-116700232.235%5Ev28%5Epc_relevant_default&utm_relevant_index=3
https://blog.csdn.net/DreamMryang/article/details/115351202

你可能感兴趣的:(mybatis,sql,logback)