P6Spy应用:Hibernate显示真正SQL(2)

下表列出了 spy.properties 配置文件中的各配置项 的名称、默认值及其意义和相关注意事项:

 

配置项名称默认值配置项意义及相关注意事项
module.log com.p6spy.engine.logging. P6LogFactory

用来拦截和记录任务应用程序的 JDBC 语句。若无配置或注释掉则无此功能。

module.outagecom.p6spy.engine.outage. P6OutageFactory

检测和记录执行时间比较长的 SQL 语句。若无配置或注释掉则无此功能。

realdriver 真正的应用系统使用的数据库驱动程序名称。
realdriver2真正的应用系统使用的第二种备用数据库驱动程序名称。
realdriver3真正的应用系统使用的第三种备用数据库驱动程序名称。
deregisterdriversfalse显示地把真正的数据库的驱动程序进行反注册掉。取值 true| false
executionthreshold

P6Log 模块执行时间设置,整数值 (以毫秒为单位),只有当超过

这个时间才进行记录 Log。

outagedetectionfalseP6Outage 模块是否记录较长时间运行的语句。取值 true|false
outagedetectioninterval

P6Outage 模块执行时间设置,整数值 (以秒为单位)),只有

当超过这个时间才进行记录 Log。

filterfalse是否过滤 Log,取值 true| false
include过滤 Log 时所包含的表名列表,以逗号分隔。
exclude过滤 Log 时所排除的表名列表,以逗号分隔。
sqlexpression过滤 Log 时的 SQL 表达式名称
autoflushtrue是否自动刷新。取值 true| false
dateformat yyyy-MM-dd HH:mm:ss:SS设置时间的格式,也就是用 Java 的 SimpleDateFormat 程序。
includecategories

显示指定过滤 Log 时包含的分类列表,取值为 error,info,batch,debug,statement,commit,

rollback,result 的各种组合。

excludecategories显示指定过滤 Log 时排队的分类列表,取值同上。
stringmatcher使用正则表达式来过滤 Log,取值为 com.p6spy.engine.common.GnuRegexMatcher 和 com.p6spy.engine.common.JakartaRegexMatcher
stacktracefalse打印堆栈跟踪信息。取值 true| false
stacktraceclass如果 stacktrace=true,则可以指定具体的类名来进行过滤。
reloadpropertiesfalse监测属性配置文件是否进行重新加载。取值 true| false
reloadpropertiesinterval60属性配置文件重新加载的时间间隔,以秒为单位。
useprefixfalse是否加上前缀,设置为 true,会加上 p6spy: 作为前缀。取值 true| false
appender com.p6spy.engine.logging. appender.FileLogger指定 Log 的 appender,与 Log4J 有点同义,取值:com.p6spy.engine.logging.appender.Log4jLogger、 com.p6spy.engine.logging.appender.StdoutLogger 和 com.p6spy.engine.logging.appender.FileLogger
logfile spy.log指定 Log 的文件名,任何适于操作系统的文件。
append true指定是否每次是增加 Log,设置为 false 则每次都会先进行清空。取值 true| false
log4j.appender.STDOUTorg.apache.log4j.ConsoleAppender当 appender 为 log4j 时采用的配置,配置如同 Log4J 的相关配置。
log4j.appender.STDOUT.layout org.apache.log4j.PatternLayout同上
log4j.appender.STDOUT. layout.ConversionPatternp6spy - %m%n同上
log4j.logger.p6spyINFO,STDOUTLog 级别的设置,取值同 Log4J 的配置
realdatasource设置数据源 DataSource 的配置名称。
realdatasourceclass设置数据源 DataSource 的类的全称。
realdatasourceproperties设置数据源 DataSource 的属性,以分号分隔。
jndicontextfactory设置 JNDI 数据源的 NamingContextFactory。
jndicontextproviderurl设置 JNDI 数据源的提供者的 URL。
jndicontextcustom设置 JNDI 数据源的一些定制信息,以分号分隔。

 

 

 

 

 

appender

Appenders allow you to specify where and how log information is output. Appenders are a flexible architecture allowing anyone to write their own output class for P6Spy. To use an appender, specify the classname of the appender to use. The current release comes with three options which are log4j, stdout, and logging to a CSV text file (default).

  • Using a CSV File— To output to a file, uncomment the FileLogger appender and specify a logfile and whether or not to append to the file or to clear the file each time:

    #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 = 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

  • Using StdOut— Uncomment the StdoutLogger as follows and all output will be sent to stdout in a CSV format:

    #appender=com.p6spy.engine.logging.appender.Log4jLogger
    appender=com.p6spy.engine.logging.appender.StdoutLogger
    #appender=com.p6spy.engine.logging.appender.FileLogger

  • Using Log4J— To output to log4j, make sure log4j is in your path, uncomment the log4j appender, and specify the desired log4j settings:

    appender=com.p6spy.engine.logging.appender.Log4jLogger
    #appender=com.p6spy.engine.logging.appender.StdoutLogger
    # appender=com.p6spy.engine.logging.appender.FileLogger

    #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.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

    If for some reason log4j cannot be initialized, the logging will go to a file called  log4jaux.log .

 

dateformat

Setting a value for dateformat changes the date format value printed in the log file. No value prints the current time in milliseconds, a useful feature for parsing the log. The date format engine is Java's SimpleDateFormat class. Refer to the SimpleDateFormat class in the JavaDocs for information on setting this value.  An example follows:

dateformat=MM-dd-yy HH:mm:ss:SS

 

Log File Format

The log file format of  spy.log   follows:

current time|execution time|category|statement SQL String|effective SQL string

  • current time The current time is obtained through System.getCurrentTimeMillis() and represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. (Refer to the J2SE documentation for further details on System.getCurrentTimeMillis().)  To change the format,  use the dateformat property described in  Common Property File Settings .
  • execution time —The time it takes for a particular method to execute. (This is not the total cost for the SQL statement.) For example, a statement SELECT * FROM MYTABLE WHERE THISCOL = ? might be executed as a prepared statement, in which the .execute() function will be measured. This is recorded as the statement category. Further, as you call .next() on the ResultSet, each .next()  call is recorded in the result category.
  • category You can manage your log by including and excluding categories, which is described in  Common Property File Settings .
  • statement SQL string —This is the SQL string passed to the statement object. If it is a prepared statement, it is the prepared statement that existed prior to the parameters being set. To see the complete statement, refer to effective SQL string.
  • effective SQL string If you are not using a prepared statement, this contains no value. Otherwise, it fills in the values of the Prepared Statement so you can see the effective SQL statement that is passed to the database. Of course, the database still sees the prepared statement, but this string is a convenient way to see the actual values  being sent to the database.

p6spy的FileLogger日志格式如上介绍的:

current time|execution time|category|statement SQL String|effective SQL string

打印出来的日志可读性不是很好,下一节我们可以定制自己的格式 。

 

以下图片中 是按我个人的格式:

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值