如何配置p6spy将其输出重定向到文件中

时间:2015-12-03 16:39:22

标签: java spring logging p6spy log4jdbc

我能够成功配置P6Spy来拦截JDBC调用并将它们输出到Eclipse的控制台。但是,我希望在文件中捕获带有绑定参数的日志。有人可以帮我问一下如何做到这一点。以下是我目前的配置:

log4j.properties

# Default log level
log4j.rootCategory=ERROR, console, FileAppender

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d [%t] %c: %m%n

# DEBUG File
log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.Threshold=DEBUG
log4j.appender.FileAppender.File=claims_logger.log
log4j.appender.FileAppender.MaxFileSize=1024KB
log4j.appender.FileAppender.MaxBackupIndex=2
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%-5p %d [%t] %c: %m%n

log4j.logger.org.apache=WARN

 log4j.logger.org.hibernate.engine.StatefulPersistenceContext.ProxyWarnLog=ERROR


log4j.logger.org.springframework.beans.factory=WARN

log4j.logger.org.mule=INFO
log4j.logger.org.mule.modules.sqs=DEBUG

log4j.logger.org.fuwt=DEBUG

spy.properties

realdriver=org.postgresql.Driver
useprefix=false
deregisterdrivers=true
module.log=com.p6spy.engine.logging.P6LogFactory

executionthreshold=

outagedetection=false
outagedetectioninterval=

filter=false

include     =

exclude     =

sqlexpression =

autoflush   = true

dateformat=yyyy-MM-dd hh:mm:ss

includecategories=

excludecategories=info,debug,result,batch

stringmatcher=

stacktrace=false

stacktraceclass=

reloadproperties=false


reloadpropertiesinterval=60


appender=com.p6spy.engine.logging.appender.StdoutLogger
append=true

log4j.additivity.p6spy=false

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=DEBUG,STDOUT    

Spring正在使用以下方法构建其数据源:

jdbc.driverClassName=com.p6spy.engine.spy.P6SpyDriver

我正在使用P6Spy 1.3版。

我读到log4jdbc是我的用例进行SQL拦截的首选方式,但是它似乎需要稍微多一点的配置,即需要根据their doc修改jdbc URL。任何关于如何在任一框架中完成参数捕获并重定向到文件的经验都表示赞赏。

提前谢谢。

2 个答案:

答案 0 :(得分:2)

您应该使用FileAppender并指定日志文件,如https://p6spy.github.io/p6spy/2.0/configandusage.html

中所示

    # specifies the appender to use for logging
    # Please note: reload means forgetting all the previously set
    # settings (even those set during runtime - via JMX)
    # and starting with the clean table 
    # (only the properties read from the configuration file)
    # (default is com.p6spy.engine.spy.appender.FileLogger)
    #appender=com.p6spy.engine.spy.appender.Slf4JLogger
    #appender=com.p6spy.engine.spy.appender.StdoutLogger
    #appender=com.p6spy.engine.spy.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 com.p6spy.engine.spy.appender.FileLogger only)
    # (default is spy.log)
    #logfile = spy.log

答案 1 :(得分:0)

事实证明,p6spy主要版本之间的appender类的命名空间之间存在差异。我们碰巧使用了FileLogger appender所在的p6spy的1.3版本:

appender=com.p6spy.engine.logging.appender.FileLogger

错误地,我们使用了以下配置,它与p6spy的2.x版本有关:

appender=com.p6spy.engine.spy.appender.FileLogger

导致错误的行为。

让我们了解的是对控制台日志的更全面检查,其中发现了以下行:

Cannot instantiate com.p6spy.engine.spy.appender.FileLogger, even on second attempt.  Logging to file log4jaux.log: java.lang.ClassNotFoundException: com.p6spy.engine.spy.appender.FileLogger