spring boot 集成mybatis使用logback打印并保存日志信息

spring boot 打印执行的sql语句

最近在学习spring boot 整合了Mybatis和druid之后总感觉少点什么东西,看了下在别的项目上用的框架,发现自己整合的东西不打印sql语句,网上找了好多方法,多半都是在application.properties 中增加配置,发现好像都没有什么作用,最后找到了正确方法。特进行记录
1.项目结构图
spring boot 集成mybatis使用logback打印并保存日志信息_第1张图片
2.由于spring boot 默认集成了logback所以直接在resources中创建logback.xml文件即可


<configuration >
    
    
    <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(%logger){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
    
    <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}}" />


    <contextName>fanxlxscontextName>

    
    <property name="LOG_PATH" value="/modellog" />
    
    <property name="APPDIR" value="logs" />

    
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/model_repatile.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/${APPDIR}/error/model_repatile-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %logger Line:%-3L - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>errorlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/model_repatile_warn.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/${APPDIR}/warn/model_repatile-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %logger Line:%-3L - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warnlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/model_repatile_ginfo.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/${APPDIR}/info/model_repatile-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %logger Line:%-3L - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>infolevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <file>${LOG_PATH}/${APPDIR}/model_repatile_debug.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${LOG_PATH}/${APPDIR}/debug/model_repatile-debug-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MBmaxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        
        <append>trueappend>
        
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %logger Line:%-3L - %msg%npattern>
            <charset>utf-8charset>
        encoder>
        
        <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>
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
            
            <charset>utf-8charset>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debuglevel>
        filter>
    appender>

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

    
    <logger name="org.apache.ibatis" level="DEBUG" />
    <logger name="java.sql" level="DEBUG" />

    
    <logger name="com.limit.mapper" level="DEBUG">logger>

    
    <root level="INFO">
        <appender-ref ref="ERROR" />
        <appender-ref ref="WARN" />
        <appender-ref ref="INFO" />
        <appender-ref ref="DEBUG" />
        
        <appender-ref ref="STDOUT" />
    root>

configuration>

3.sql已经打印出来了,保存的日志文件在项目所在盘符的根目录上
这里写图片描述

4.最近在试着用idea写项目所以eclicpse 彩色代码没有进行测试,如果不需要使用可以将,彩色日志部分删除或者注释即可,另外想使用彩色日志可以使用sts(集成了spring 工具的ide 和eclipse 没有本质区别),在启动类向右键run as ->spring boot app即可,感兴趣的可以试试给个连接 官网连接 这个连接下的比较慢,在附个百度云连接吧
https://pan.baidu.com/s/15qZPmsWAmBvqS3jucbk9Hg 密码 yf2d

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