美文网首页
使用 p6spy 在 SpringBoot 中打印&监控sql

使用 p6spy 在 SpringBoot 中打印&监控sql

作者: doublinglee | 来源:发表于2020-07-04 13:06 被阅读0次

引入maven配置

      <groupId>p6spy</groupId>

      <artifactId>p6spy</artifactId>

      <version>3.7.0</version>

    </dependency>

注意:推荐使用3.7.0 ,较新版本支持自定义sql格式

spy.properties 配置

找到driverlist,配置自己的数据库真实的驱动,支持多个

driverlist=com.mysql.cj.jdbc.Driver,org.h2.Driver

找到dateformat,

这里默认设置的时间格式和是MM-dd-yy HH:mm:ss:SS,建议调整为yyyy-MM-dd HH:mm:ss:SS

appender配置,定义日志输出的位置,这里我选择的是标准输出

appender=com.p6spy.engine.spy.appender.StdoutLogger

logMessageFormat配置,默认是SingleLineFormat,这种格式打印出来的sql包含sql变量填充前 也包括填充后的,看起来比较麻烦,我一般调整为

com.p6spy.engine.spy.appender.CustomLineFormat  这个有些过低的版本不支持,配置时注意一下

customLogMessageFormat 如果使用CustomLineFormat,这里是日志格式的配置模式,我用的是默认,可以参考

# %(connectionId) the id of the connection

# %(currentTime) the current time expressing in milliseconds

# %(executionTime) the time in milliseconds that the operation took to complete

# %(category) the category of the operation# %(effectiveSql) the SQL statement as submitted to the driver

# %(effectiveSqlSingleLine) the SQL statement as submitted to the driver, with all new lines removed

# %(sql) the SQL statement with all bind variables replaced with actual values

# %(sqlSingleLine) the SQL statement with all bind variables replaced with actual values, with all new lines removed

数据源配置

需要有两个参数调整

spring.datasource.driver-class-name 驱动名称,这里直接改成 com.p6spy.engine.spy.P6SpyDriver

spring.datasource.url:一般在jdbc:后面加上p6spy, 如:

H2:jdbc:p6spy:h2:mem:testdb

mysql:jdbc:p6spy:mysql://xxxx

相关文章

网友评论

      本文标题:使用 p6spy 在 SpringBoot 中打印&监控sql

      本文链接:https://www.haomeiwen.com/subject/pmigdftx.html