SSM框架搭建(三)--整合p6spy

本文档详细介绍了如何在SSM框架中整合p6spy,用于数据库监控。通过设置p6spy的配置文件,可以定制日志格式、过滤日志内容、设置执行阈值等。此外,还提供了自定义的日志打印实现,增强了日志输出的可读性。
摘要由CSDN通过智能技术生成

强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan

描述

普通情况下,控制台打印出的sql是带?的,开发人员在自己调试的过程中遇到bug是再常见不过了,如果想看一下程序打出来的sql,发现参数的位置都是?,是不是很不爽,若想在数据库中执行sql,还需要手动修改sql的参数,真是麻烦,使用p6spy可以将sql格式化然后打出,打出的sql不再是带有?的,而是可以直接拷贝到数据库执行的。整合步骤如下:

添加jar包依赖

在service层添加依赖: ``` p6spy p6spy 3.7.0

</font>
<h1 id="背景"><table><tbody><tr><td bgcolor="#008B8B" height="40px"><font size="6px" face="华文新魏" color="white">自定义输出格式</font></td></tr></tbody></table></h1>
<font size="4px" face="楷体" font-weight="bold">

package com.zh.ssm.p6spy;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

import java.text.SimpleDateFormat;
import java.util.Date;

/**

  • p6spy打印日志输出格式修改
  • 1.只打印最终执行的sql.
  • 2.sql换到下一行
  • 3.结尾处增加分号,以标示sql结尾

*/

public class P6SpyLogger implements MessageFormattingStrategy {
private SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss:SSS”);

public P6SpyLogger() {
}
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {
    return !"".equals(sql.trim())?this.format.format(new Date()) + " | took " + elapsed + "ms | " + category + " | connection " + connectionId + "\n " + sql + ";":"";
}

}

</font>
<h1 id="背景"><table><tbody><tr><td bgcolor="#008B8B" height="40px"><font size="6px" face="华文新魏" color="white">配置spy.properties</font></td></tr></tbody></table></h1>
<font size="4px" face="楷体" font-weight="bold">
在service层的classpath下新建文件spy.properties,内容如下:

#%L

P6Spy

%%

Copyright © 2013 P6Spy

%%

Licensed under the Apache License, Version 2.0 (the “License”);

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an “AS IS” BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

#L%

#################################################################

P6Spy Options File

See documentation for detailed instructions

http://p6spy.github.io/p6spy/2.0/configandusage.html

#################################################################
#################################################################

MODULES

Module list adapts the modular functionality of P6Spy.

Only modules listed are active.

(default is com.p6spy.engine.logging.P6LogFactory and

com.p6spy.engine.spy.P6SpyFactory)

Please note that the core module (P6SpyFactory) can’t be

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值