p6spy:idea控制台打印SQL语句等信息

本是古典 何须时尚 2024-03-23 15:25 229阅读 0赞

官网: https://p6spy.readthedocs.io/en/latest/install.html

依赖

  1. <dependency>
  2. <groupId>p6spy</groupId>
  3. <artifactId>p6spy</artifactId>
  4. <p6spy.version>3.9.1</p6spy.version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.hutool</groupId>
  8. <artifactId>hutool-all</artifactId>
  9. <hutool.version>5.8.6</hutool.version>
  10. </dependency>

application.yml

  1. spring:
  2. datasource:
  3. url: jdbc:p6spy:mysql://localhost:3306/test?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true
  4. username: root
  5. password: root
  6. driver-class-name: com.p6spy.engine.spy.P6SpyDriver

注意

  1. 替换JDBC Drivercom.p6spy.engine.spy.P6SpyDriver
  2. 在原来urljdbc:后面添加p6spy:;如:jdbc:p6spy:mysql://127.0.0.1:3306或jdbc:p6spy:oracle:thin:@localhost:1521:ORCL
  3. 添加p6spy的配置文件spy.properties

spy.properties

  1. # 指定应用的日志拦截模块,默认为com.p6spy.engine.spy.P6SpyFactory
  2. modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
  3. # 如果使用了Mybatis-Plus建议使用下面
  4. # modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
  5. # 自带的日志格式 (default is com.p6spy.engine.spy.appender.FileLogger)
  6. #appender=com.p6spy.engine.spy.appender.StdoutLogger
  7. #appender=com.p6spy.engine.spy.appender.FileLogger
  8. appender=com.p6spy.engine.spy.appender.Slf4JLogger
  9. # 如果使用了Mybatis-Plus建议使用下面
  10. # appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
  11. # 设置使用p6spy driver来做代理
  12. deregisterdrivers=true
  13. # ??JDBC URL??
  14. useprefix=true
  15. #显示指定过滤 Log 时排队的分类列表,取值: error, info, batch, debug, statement,
  16. #commit, rollback, result and resultset are valid values
  17. # (默认 info,debug,result,resultset,batch)
  18. excludecategories=info,debug,result,commit,resultset
  19. # 日期格式
  20. dateformat=yyyy-MM-dd HH:mm:ss
  21. # 是否开启慢SQL记录 默认false
  22. outagedetection=true
  23. # 模块执行时间设置,整数值 (以秒为单位)),只有当超过这个时间才进行记录 Log。 默认30s
  24. outagedetectioninterval=2

自定义在idea控制台打印sql的形式

第一种方式
实现MessageFormattingStrategy接口

  1. import cn.hutool.db.sql.SqlUtil;
  2. import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
  3. import lombok.extern.slf4j.Slf4j;
  4. @Slf4j
  5. public class P6spyLogFormatStrategy implements MessageFormattingStrategy {
  6. /**
  7. * 日志格式化方式(打印SQL日志会进入此方法,耗时操作,生产环境不建议使用)
  8. *
  9. * @param connectionId: 连接ID
  10. * @param now: 当前时间
  11. * @param elapsed: 花费时间
  12. * @param category: 类别
  13. * @param prepared: 预编译SQL
  14. * @param sql: 最终执行的SQL
  15. * @param url: 数据库连接地址
  16. * @return 格式化日志结果
  17. **/
  18. @Override
  19. public String formatMessage(int connectionId, String now, long elapsed,
  20. String category, String prepared, String sql,
  21. String url) {
  22. log.info("SQL耗时【{}毫秒】 SQL-> {}", elapsed, SqlUtil.formatSql(sql));
  23. return "";
  24. }
  25. }
  26. # 在spy.properties中加上下面即可
  27. # 自定义idea控制台sql日志打印形式
  28. logMessageFormat=con.wuxian.conf.p6spy.P6spyLogFormatStrategy

第二种方式

  1. # 在spy.properties中加上下面即可
  2. # 指定idea控制台sql日志输出样式 默认为
  3. # com.p6spy.engine.spy.appender.SingleLineFormat , 单行输出 不格式化语句
  4. #logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
  5. # 也可以采用 com.p6spy.engine.spy.appender.CustomLineFormat 来自定义输出样式, 默认值是%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine)
  6. # 可用的变量为:
  7. # %(connectionId) connection id
  8. # %(currentTime) 当前时间
  9. # %(executionTime) 执行耗时
  10. # %(category) 执行分组
  11. # %(effectiveSql) 提交的SQL 换行
  12. # %(effectiveSqlSingleLine) 提交的SQL 不换行显示
  13. # %(sql) 执行的真实SQL语句,已替换占位
  14. # %(sqlSingleLine) 执行的真实SQL语句,已替换占位 不换行显示
  15. customLogMessageFormat=%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine)

对输出的日志文件控制

  1. # 在spy.properties中加上下面即可
  2. # 指定 Log 的文件名 默认 spy.log
  3. logfile=spy.log
  4. # 指定是否每次是增加 Log,设置为 false 则每次都会先进行清空,默认true
  5. append=false

打印堆栈跟踪信息

  1. # 在spy.properties中加上下面即可
  2. # 打印堆栈跟踪信息 默认flase
  3. stacktrace=ture
  4. # 如果 stacktrace=true,则可以指定具体的类名来进行过滤。
  5. #stacktraceclass=

对一些情况不打印日志进行过滤

  1. # 过滤 Log 时所包含的表名列表,以逗号分隔 默认为空
  2. #include=
  3. # 过滤 Log 时所排除的表名列表,以逗号分隔 默认为空
  4. #exclude=
  5. # 过滤 Log 时的 SQL 正则表达式名称 默认为空
  6. #sqlexpression=

发表评论

表情:
评论列表 (有 0 条评论,229人围观)

还没有评论,来说两句吧...

相关阅读