1闹啦、添加依賴
<dependency>
? ? <groupId>p6spy</groupId>
? ? <artifactId>p6spy</artifactId>
? ? <version>3.7.0</version>
</dependency>
2、新建spy.properties
# 自定義日志打印
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime) | SQL耗時(shí): %(executionTime) ms | 連接信息: %(category)-%(connectionId) | 執(zhí)行語句: %(sql)
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定義日志打印
#logMessageFormat=org.pkaq.core.log.P6SpyLogger
# 使用日志系統(tǒng)記錄sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置記錄Log例外
excludecategories=info,debug,result,batc,resultset
# 設(shè)置使用p6spy driver來做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 實(shí)際驅(qū)動(dòng)
driverlist=com.mysql.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標(biāo)準(zhǔn) 秒
outagedetectioninterval=2
也可以自定義輸出格式
3、修改數(shù)據(jù)庫配置文件
spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql:///houserent?serverTimezone=GMT%2B8
http://www.reibang.com/p/199d5f78aa66? ?
(文件內(nèi)容太多了杉允,僅附上幾處重要的)
#################################################################
# 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的屬性必須配置,如果不配置般贼,P6SPY將不起任何作用瘸恼,典型配置:
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.? ? ? ? ? ? ? ? #
#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #
#################################################################
#第二:數(shù)據(jù)庫驅(qū)動(dòng)配置,你懂的似谁,不多說了
# 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=
#第三:appender配置傲绣,一般分為三種
#specifies the appender to use for logging
#appender=com.p6spy.engine.logging.appender.Log4jLogger
#控制臺(tái)
#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? ? = 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.logger.p6spy=INFO,STDOUT
作者:Crazy_Coder
鏈接:http://www.reibang.com/p/199d5f78aa66
來源:簡(jiǎn)書
簡(jiǎn)書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處巩踏。