p6spy监测SQL工具配置

1.maven pom.xml 配置
<!-- p6spy检测SQL执行详情 -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>1.3</version>
</dependency>
2.放置spy.propotities文件

放在maven项目下的

放在普通项目下的

spy.properties
spring中配置数据源

将datasource包装一层,用来拦截sql

spy.properties文件
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/


-->#################################################################
# MODULES #
# #
# Modules provide the P6Spy functionality. If a module, such #
# as module_log is commented out, that functionality will not #
# be available. If it is not commented out (if it is active), #
# the functionality will be active. #
# #
# Values set in Modules cannot be reloaded using the #
# reloadproperties variable. Once they are loaded, they remain #
# in memory until the application is restarted. #
# #
#################################################################


module.log=com.p6spy.engine.logging.P6LogFactory
#module.outage=com.p6spy.engine.outage.P6OutageFactory


#################################################################
# REALDRIVER(s) #
# #
# In your application server configuration file you replace the #
# "real driver" name with com.p6spy.engine.P6SpyDriver. This is #
# where you put the name of your real driver P6Spy can find and #
# register your real driver to do the database work. #
# #
# If your application uses several drivers specify them in #
# realdriver2, realdriver3. See the documentation for more #
# details. #
# #
# Values set in REALDRIVER(s) cannot be reloaded using the #
# reloadproperties variable. Once they are loaded, they remain #
# in memory until the application is restarted. #
# #
#################################################################


# the hsqldb open source driver
#realdriver=org.hsqldb.jdbcDriver


# oracle driver
#realdriver=oracle.jdbc.driver.OracleDriver


# mysql Connector/J driver
realdriver=com.mysql.jdbc.Driver


# informix driver
# realdriver=com.informix.jdbc.IfxDriver


# ibm db2 driver
# realdriver=COM.ibm.db2.jdbc.net.DB2Driver


# the mysql open source driver
# realdriver=org.gjt.mm.mysql.Driver


#specifies another driver to use
realdriver2=
#specifies a third driver to use
realdriver3=


#the DriverManager class sequentially tries every driver that is
#registered to find the right driver. In some instances, it's possible to
#load up the realdriver before the p6spy driver, in which case your connections
#will not get wrapped as the realdriver will "steal" the connection before
#p6spy sees it. Set the following property to "true" to cause p6spy to
#explicitily deregister the realdrivers
deregisterdrivers=true


################################################################
# P6LOG SPECIFIC PROPERTIES #
################################################################
# no properties currently available


################################################################
# EXECUTION THRESHOLD PROPERTIES #
################################################################
# This feature applies to the standard logging of P6Spy. #
# While the standard logging logs out every statement #
# regardless of its execution time, this feature puts a time #
# condition on that logging. Only statements that have taken #
# longer than the time specified (in milliseconds) will be #
# logged. This way it is possible to see only statements that #
# have exceeded some high water mark. #
# This time is reloadable. #
#
# executionthreshold=integer time (milliseconds)
#
executionthreshold=


################################################################
# P6OUTAGE SPECIFIC PROPERTIES #
################################################################
# Outage Detection
#
# This feature detects long-running statements that may be indicative of
# a database outage problem. If this feature is turned on, it will log any
# statement that surpasses the configurable time boundary during its execution.
# When this feature is enabled, no other statements are logged except the long
# running statements. The interval property is the boundary time set in seconds.
# For example, if this is set to 2, then any statement requiring at least 2
# seconds will be logged. Note that the same statement will continue to be logged
# for as long as it executes. So if the interval is set to 2, and the query takes
# 11 seconds, it will be logged 5 times (at the 2, 4, 6, 8, 10 second intervals).
#
# outagedetection=true|false
# outagedetectioninterval=integer time (seconds)
#
outagedetection=false
outagedetectioninterval=


################################################################
# COMMON PROPERTIES #
################################################################


# comma separated list of tables to include
include =
# comma separated list of tables to exclude
exclude =


# sql expression to evaluate if using regex
sqlexpression =


# filter what is logged
filter=false


# turn on tracing
trace = true
autoflush = true


# sets the date format using Java's SimpleDateFormat routine
dateformat=


#list of categories to explicitly include
includecategories=


#list of categories to exclude: error, info, batch, debug, statement,
#commit, rollback and result are valid values
#excludecategories=
excludecategories=info,debug,result,batch


#allows you to use a regex engine or your own matching engine to determine
#which statements to log
#
#stringmatcher=com.p6spy.engine.common.GnuRegexMatcher
#stringmatcher=com.p6spy.engine.common.JakartaRegexMatcher
stringmatcher=


# prints a stack trace for every statement logged
stacktrace=false
# if stacktrace=true, specifies the stack trace to print
stacktraceclass=


# determines if property file should be reloaded
reloadproperties=false
# determines how often should be reloaded in seconds
reloadpropertiesinterval=60


#if=true then url must be prefixed with p6spy:
#为什么每条sql打印两遍设为true就只有一条了!
useprefix=true


#specifies the appender to use for logging
appender=com.p6spy.engine.logging.appender.Log4jLogger
#appender=com.p6spy.engine.logging.appender.StdoutLogger
#appender=com.p6spy.engine.logging.appender.FileLogger


# name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log) (used for file logger only)
#logfile = c:/spy.log


# append to the p6spy log file. if this is set to false the
# log file is truncated every time. (file logger only)
append=true


#The following are for log4j logging only
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n


log4j.appender.F=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.F.File=c:/spy.log
log4j.appender.F.layout=org.apache.log4j.PatternLayout
#log4j.appender.F.layout.ConversionPattern=%p %t %c - %m%n


#log4j.appender.CHAINSAW_CLIENT=org.apache.log4j.net.SocketAppender
#log4j.appender.CHAINSAW_CLIENT.RemoteHost=localhost
#log4j.appender.CHAINSAW_CLIENT.Port=4445
#log4j.appender.CHAINSAW_CLIENT.LocationInfo=true


log4j.logger.p6spy=INFO,STDOUT,F


#################################################################
# DataSource replacement #
# #
# Replace the real DataSource class in your application server #
# configuration with the name com.p6spy.engine.spy.P6DataSource,#
# then add the JNDI name and class name of the real #
# DataSource here #
# #
# Values set in this item cannot be reloaded using the #
# reloadproperties variable. Once it is loaded, it remains #
# in memory until the application is restarted. #
# #
#################################################################
#realdatasource=/RealMySqlDS
#realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource


#################################################################
# DataSource properties #
# #
# If you are using the DataSource support to intercept calls #
# to a DataSource that requires properties for proper setup, #
# define those properties here. Use name value pairs, separate #
# the name and value with a semicolon, and separate the #
# pairs with commas. #
# #
# The example shown here is for mysql #
# #
#################################################################
#realdatasourceproperties=port;3306,serverName;myhost,databaseName;jbossdb,foo;bar




#################################################################
# JNDI DataSource lookup #
# #
# If you are using the DataSource support outside of an app #
# server, you will probably need to define the JNDI Context #
# environment. #
# #
# If the P6Spy code will be executing inside an app server then #
# do not use these properties, and the DataSource lookup will #
# use the naming context defined by the app server. #
# #
# The two standard elements of the naming environment are #
# jndicontextfactory and jndicontextproviderurl. If you need #
# additional elements, use the jndicontextcustom property. #
# You can define multiple properties in jndicontextcustom, #
# in name value pairs. Separate the name and value with a #
# semicolon, and separate the pairs with commas. #
# #
# The example shown here is for a standalone program running on #
# a machine that is also running JBoss, so the JDNI context #
# is configured for JBoss (3.0.4). #
# #
#################################################################
#jndicontextfactory=org.jnp.interfaces.NamingContextFactory
#jndicontextproviderurl=localhost:1099
#jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.nameing:org.jnp.interfaces


#jndicontextfactory=com.ibm.websphere.naming.WsnInitialContextFactory
#jndicontextproviderurl=iiop://localhost:900


配置完毕!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 p6spy 来保存 SQL 到数据库。p6spy 是一个用于监控和记录 JDBC 驱动程序生成的 SQL 语句的工具。它提供了一个代理驱动程序,可以将 SQL 输出到日志文件或数据库中。 以下是一些步骤来配置 p6spy 来保存 SQL 到数据库: 1. 导入 p6spy 的依赖:在你的项目中添加 p6spy 的依赖,例如 Maven 中的以下代码片段: ``` <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency> ``` 2. 配置数据库连接:在你的应用程序中,将数据库连接的驱动程序类名设置为 p6spy 的驱动程序类名。例如,对于 MySQL 数据库: ``` jdbc.driver=com.p6spy.engine.spy.P6SpyDriver ``` 3. 配置 p6spy:在你的应用程序的配置文件中,添加 p6spy配置项。例如,在 Spring Boot 应用程序的 `application.properties` 文件中添加以下配置: ``` spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/mydatabase spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect ``` 4. 配置日志输出:在 p6spy配置文件中,指定你想要将 SQL 输出日志保存到的目标位置,可以是日志文件或数据库。例如,在 Spring Boot 应用程序的 `application.properties` 文件中添加以下配置: ``` # 将 SQL 输出到文件 spy.logFile=/path/to/log/file.log # 将 SQL 输出到数据库 spy.appender=com.p6spy.engine.spy.appender.Slf4JLogger ``` 这样,p6spy 就会拦截应用程序生成的 SQL 并将其保存到指定的日志文件或数据库中。你可以根据需要调整 p6spy配置和日志输出方式。希望这对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值