SpringBoot+Mybatis logback不打印SQL解决方案
最近项目,由于测试提出需要将日志按天滚动日志,所以用了logback,日志打印工具。
但是由于使用了mybatis-config.xml 一直不打印SQL。最后将
<setting name="logImpl" value="STDOUT_LOGGING" />
这行注释,并且在application.properties里添加
logging.level.com.**.**.**.schedule.mapper=DEBUG
就可以了,以下是完整的配置。
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties> <property name="dialect" value="mysql"/> </properties> <settings> <!-- 开启驼峰匹配 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 这个配置使全局的映射器启用或禁用缓存。系统默认值是true,设置只是为了展示出来 --> <setting name="cacheEnabled" value="true"/> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 系统默认值是true,设置只是为了展示出来 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)。 系统默认值是true,设置只是为了展示出来 --> <setting name="multipleResultSetsEnabled" value="true"/> <!--使用列标签代替列名。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。 系统默认值是true,设置只是为了展示出来 --> <setting name="useColumnLabel" value="true"/> <!--允许 JDBC 支持生成的键。需要适合的驱动。如果设置为 true 则这个设置强制生成的键被使用,尽管一些驱动拒绝兼容但仍然有效(比如 Derby)。 系统默认值是false,设置只是为了展示出来 --> <setting name="useGeneratedKeys" value="false"/> <!--配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。BATCH 执行器重用语句和批量更新 系统默认值是SIMPLE,设置只是为了展示出来 --> <setting name="defaultExecutorType" value="SIMPLE"/> <!--设置超时时间,它决定驱动等待一个数据库响应的时间。 系统默认值是null,设置只是为了展示出来 --> <setting name="defaultStatementTimeout" value="25000"/> <setting name="callSettersOnNulls" value="true"/> <!-- 返回空字段 --> <!-- 打印查询语句 --> <!-- <setting name="logImpl" value="STDOUT_LOGGING" />--> </settings> <!-- 分页助手 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 数据库方言 --> <property name="dialect" value="mysql" /> <property name="offsetAsPageNum" value="true" /> <!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 --> <property name="rowBoundsWithCount" value="true" /> <property name="pageSizeZero" value="true" /> <property name="reasonable" value="true" /> </plugin> </plugins> </configuration>
logback相关:
logback.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="30 seconds" debug="true"> <property file ="../config/logback/logback.properties" /> <include file="../config/logback/logback-prod.xml"/> <root level="INFO"> <appender-ref ref="ASYNC_ROLLING_INFO" /> <appender-ref ref="ASYNC_ROLLING_DEBUG" /> <appender-ref ref="ASYNC_ROLLING_WARN" /> <appender-ref ref="ASYNC_ROLLING_ERROR" /> </root> </configuration>
logback-prod.xml:
<?xml version="1.0" encoding="UTF-8"?> <included> <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${info.file.path}</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>../log/info-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> </appender> <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${debug.file.path}</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${base.path}/debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> </appender> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${error.file.path}</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${base.path}/error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>${log.pattern}</pattern> </encoder> </appender> <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${warn.file.path}</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${base.path}/warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder>R <pattern>${log.pattern}</pattern> </encoder> </appender> <appender name="sql" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${sql.file.path}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>${base.path}/sql.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <MaxHistory>30</MaxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 100MB --> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss} [%t] [%X{traceId}] %5p %c:%L] %m%n</pattern> </encoder> </appender> <logger name="com.**.**.**.schedule.mapper" level="DEBUG" additivity="false"> <appender-ref ref="sql" /> </logger> <logger name="org.mybatis" level="DEBUG" additivity="false" > <appender-ref ref="sql" /> </logger> <appender name="ASYNC_ROLLING_INFO" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="INFO" /> </appender> <appender name="ASYNC_ROLLING_DEBUG" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="DEBUG" /> </appender> <appender name="ASYNC_ROLLING_WARN" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="WARN" /> </appender> <appender name="ASYNC_ROLLING_ERROR" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="ERROR" /> </appender> </included>
logback.properties
log.pattern = [%date{ISO8601}] [%-5level] [%X{requestId}] [%X{rocketmqId}] [%logger] - %msg %rootException %n log.level.pattern = [%date{ISO8601}] [%-5level] [%X{requestId}] [%X{rocketmqId}] [%logger] - %msg %rootException %n info.file.path = ../log/info.log debug.file.path = ../log/debug.log error.file.path = ../log/error.log sum.file.path = ../log/sum.log warn.file.path = ../log/warn.log sql.file.path = ../log/sql.log base.path=../log
文章为原创
谢谢观看