下表列出了 spy.properties 配置文件中的各配置项 的名称、默认值及其意义和相关注意事项:
配置项名称 | 默认值 | 配置项意义及相关注意事项 |
---|---|---|
module.log | com.p6spy.engine.logging. P6LogFactory | 用来拦截和记录任务应用程序的 JDBC 语句。若无配置或注释掉则无此功能。 |
module.outage | com.p6spy.engine.outage. P6OutageFactory | 检测和记录执行时间比较长的 SQL 语句。若无配置或注释掉则无此功能。 |
realdriver | 真正的应用系统使用的数据库驱动程序名称。 | |
realdriver2 | 真正的应用系统使用的第二种备用数据库驱动程序名称。 | |
realdriver3 | 真正的应用系统使用的第三种备用数据库驱动程序名称。 | |
deregisterdrivers | false | 显示地把真正的数据库的驱动程序进行反注册掉。取值 true| false |
executionthreshold | P6Log 模块执行时间设置,整数值 (以毫秒为单位),只有当超过 这个时间才进行记录 Log。 | |
outagedetection | false | P6Outage 模块是否记录较长时间运行的语句。取值 true|false |
outagedetectioninterval | P6Outage 模块执行时间设置,整数值 (以秒为单位)),只有 当超过这个时间才进行记录 Log。 | |
filter | false | 是否过滤 Log,取值 true| false |
include | 过滤 Log 时所包含的表名列表,以逗号分隔。 | |
exclude | 过滤 Log 时所排除的表名列表,以逗号分隔。 | |
sqlexpression | 过滤 Log 时的 SQL 表达式名称 | |
autoflush | true | 是否自动刷新。取值 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 | |
stacktrace | false | 打印堆栈跟踪信息。取值 true| false |
stacktraceclass | 如果 stacktrace=true,则可以指定具体的类名来进行过滤。 | |
reloadproperties | false | 监测属性配置文件是否进行重新加载。取值 true| false |
reloadpropertiesinterval | 60 | 属性配置文件重新加载的时间间隔,以秒为单位。 |
useprefix | false | 是否加上前缀,设置为 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.STDOUT | org.apache.log4j.ConsoleAppender | 当 appender 为 log4j 时采用的配置,配置如同 Log4J 的相关配置。 |
log4j.appender.STDOUT.layout | org.apache.log4j.PatternLayout | 同上 |
log4j.appender.STDOUT. layout.ConversionPattern | p6spy - %m%n | 同上 |
log4j.logger.p6spy | INFO,STDOUT | Log 级别的设置,取值同 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=truelog4j.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
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 ”
打印出来的日志可读性不是很好,下一节我们可以定制自己的格式 。
以下图片中 是按我个人的格式: