• p6spy sql 执行记录


    学习新的技能中,发现一个插件,能够比较容易的统计 sql 执行情况 

    p6spy

    首先做为拓展技能,该软件属于开源软件,能够实现的目的是拦截 sql ,将sql 执行情况自定义输出/统计

    在 spring boot 中使用 p6spy 需要先引入依赖

    <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.1</version>
    </dependency>

    引入依赖后,需要在 resource 目录下增加一个 spy.properties 配置文件,目的是指定相关的属性

    # p6spy配置,文档 https://p6spy.readthedocs.io/en/latest/configandusage.html
    
    module.log=com.p6spy.engine.logging.P6LogFactory
    
    # 使用日志系统记录 sql
    appender=com.p6spy.engine.spy.appender.Slf4JLogger
    
    # 自定义日志打印
    logMessageFormat=com.bai.common.configure.P6spySqlFormatConfigure
    
    append=true
    
    # 开启过滤
    filter=true
    # 包含 QUARTZ
    exclude=QUARTZ,select 1

    配置中指定了自定义打印日志的配置类  P6spySqlFormatConfigure ,需要继承 MessageFormattingStrategy ,并重写 SQL 输出格式

    public class P6spySqlFormatConfigure implements MessageFormattingStrategy {
        @Override
        public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
            return StringUtils.isNotBlank(sql) ? DateUtil.formatFullTime(LocalDateTime.now(), DateUtil.FULL_TIME_SPLIT_PATTERN)
                    + " | 耗时 " + elapsed + " ms | SQL 语句:" + StringUtils.LF + sql.replaceAll("[\s]+", StringUtils.SPACE) + ";" : StringUtils.EMPTY;
        }
    }

    最后,即需要在连接数据库 mysql 的 driver-class-name ,以及 url 中指定 p6spy ,连接配置如下

    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://ip:port/db?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8

    项目地址:https://www.oschina.net/p/p6spy?hmsr=aladdin1e1

    p6spy 博客:https://my.oschina.net/hutaishi/blog/3020251

  • 相关阅读:
    mysql执行sql脚本
    Eclipse Memory Analyzer 进行堆转储文件分析
    JAVA字符串格式化-String.format()
    rpm 使用
    md5sum 使用
    Spring Security 初探
    java工厂模式
    Linux 定时任务
    Java Map 知识
    【转】MVC 比较
  • 原文地址:https://www.cnblogs.com/bytecodebuffer/p/14343839.html
Copyright © 2020-2023  润新知