前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >p6使用教程_p6slite使用说明书

p6使用教程_p6slite使用说明书

作者头像
全栈程序员站长
发布2022-11-08 15:24:26
4160
发布2022-11-08 15:24:26
举报
文章被收录于专栏:全栈程序员必看

最近开发中用到 spring-data-jpa + hibernate 的持久层框架,在调试过程中,日志记录的hibernate SQL执行语句无法显示传入的参数,所以上github上搜索了一番,发现了p6spy这个框架,此框架能够无缝地拦截和记录数据库的执行语句,而不会对现有应用程序进行代码更改。下面介绍一下p6spy的简单配置和使用。

源码

https://github.com/p6spy/p6spy

导包

http://mvnrepository.com/artifact/p6spy/p6spy

代码语言:javascript
复制
<dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.7.0</version>
</dependency>
配置

1、复制源码中 p6spy/src/main/assembly/individualFiles/spy.properties 文件到Maven项目的resource目录下 2、根据需要配置 spy.properties 选项值

代码语言:javascript
复制
#项目数据库驱动
driverlist=com.mysql.jdbc.Driver
#日期格式
dateformat=yyyy-MM-dd HH:mm:ss
#sql输出样式(此为我自定义的)
logMessageFormat=com.p6spy.engine.spy.appender.PrettySqlMultiLineFormat

#sql输出方式
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
appender=com.p6spy.engine.spy.appender.StdoutLogger
#appender=com.p6spy.engine.spy.appender.FileLogger

3、项目数据源配置 jdbc.driver 替换为 com.p6spy.engine.spy.P6SpyDriver jdbc.url 替换为 jdbc:p6spy:mysql:/xxx

问题

1、原生的sql语句样式是单行的,如何格式化 改动源码,创建一个类自定义输出格式,重新打包发布到自己的本地库

代码语言:javascript
复制
package com.p6spy.engine.spy.appender;

import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;
import org.hibernate.engine.jdbc.internal.Formatter;

/** * 优化sql输出格式,采用hibernate的 Formatter */
public class PrettySqlMultiLineFormat implements MessageFormattingStrategy { 
   

    private static final Formatter formatter;

    static {
        formatter = new BasicFormatterImpl();
    }

  /** * Formats a log message for the logging module * * @param connectionId the id of the connection * @param now the current ime expressing in milliseconds * @param elapsed the time in milliseconds that the operation took to complete * @param category the category of the operation * @param prepared the SQL statement with all bind variables replaced with actual values * @param sql the sql statement executed * @return the formatted log message */
    @Override
    public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql) {
      return "\n#" + now + " | took " + elapsed + "ms | " + category + " | connection " + connectionId + formatter.format(sql) +";";
    }
}

spy.properties 中 logMessageFormat=com.p6spy.engine.spy.appender.PrettySqlMultiLineFormat

2、与日志框架的结合 我项目的日志框架是 slf4j + log4j2 的组合,不需要改动日志配置,p6spy可以无缝结合。

3、精简配置 如果只是自己开发调试时用,可以对p6spy定制,从github上下载源码,直接把spy.properties放到源码项目的resource目录下进行配置,然后发布到本地Maven库中,项目要用时只要在pom.xml中引入依赖就,修改数据库驱动和地址,不需要在项目中配置spy.properties文件了。

这里写图片描述
这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185320.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月6日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 源码
  • 导包
  • 配置
  • 问题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档