古越剑箫

学习是一种习惯

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

在看本文之前你需要会使用logback或者log4j2基本配置,可以参看前期文章

log4j2    logback

 

1、druid官方出的方案,本文是在官方方案上进一步优化,Druid中使用log4j2进行日志输出

2、在使用mybatis作为ORM组件,打印SQL日志需要指定xxxx.Mapper日志级别是DEBUG级别,因为多服务下包名是不一样的,这样做是比较不方便无法做到通用。

3、官方的描述中需要开启日志打印

# 配置日志输出
spring.datasource.druid.filter.slf4j.enabled=true

 

image

 

标红的是不需要打印,查看源码,com.alibaba.druid.filter.logging.Slf4jLogFilter

 

image

发现默认情况下都是true配置,因此我们需要将不需要打印的日志改为false

image

 

源码

if (statementPrepareAfterLogEnable && isStatementLogEnabled()) {
            statementLog("{conn-" + statement.getConnectionProxy().getId() + ", pstmt-" + statement.getId()
                         + "} created. " + statement.getSql());
        }

 

我们将statementPrepareAfterLogEnable  配置为false即可

spring.datasource.druid.filter.slf4j.enabled=true
spring.datasource.druid.filter.slf4j.statement-prepare-after-log-enabled=false

 

日志输出如下,已经没有created.

image

 

 

同理,closed也是如此配置,

image

 

#输出sql日志
spring.datasource.druid.filter.slf4j.enabled=true
spring.datasource.druid.filter.slf4j.statement-prepare-after-log-enabled=false
spring.datasource.druid.filter.slf4j.statement-close-after-log-enabled=false

 

最终输出结果

image

 

 

关于sql换行的问题,我DEBUG跟踪了下,发现最原始的SQL来源于

image

具体追踪到mybatis的源码

org.apache.ibatis.mapping.MappedStatement#getBoundSql

image

org.apache.ibatis.scripting.xmltags.DynamicSqlSource#getBoundSql

image

这里就不在深究,druid官方提供的 statementLogSqlPrettyFormat 变量,看样子是日志格式化,但是这个变量并没有被使用。

image

posted on 2020-09-15 12:09  古越剑箫  阅读(21271)  评论(0编辑  收藏  举报