log4jdbc可以用来查看实际执行的sql,记录每条sql的执行时间,查看connection相关信息,总之想要记录jdbc相关的log,都可以使用;
1. 下载log4jdbc: https://code.google.com/p/log4jdbc/downloads/list
2. 所需jar包:
log4j-xxx.jar
slf4j-api-xxx.jar
slf4j-log4j12-xxx.jar
3.配置:
1)将datasource的driverClass设置为:net.sf.log4jdbc.DriverSpy
2)将db url的前缀由jdbc: 变成jdbc:log4jdbc:
e.g:
original:
jdbc:oracle:thin:@hostname:port:sid
current:
jdbc:log4jdbc:oracle:thin:@hostname:port:sid
3)update log4j.properties
e.g:
log4j.rootLogger=stdout,sqltiming,sql
# stdout is set to be a stdoutAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}] - %m%n
log4j.appender.sql=org.apache.log4j.FileAppender
log4j.appender.sql.File=logs/sql.log
log4j.appender.sql.Append=false
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p[%t](%C:%L) -%m%n
#log4j.logger.jdbc.sqlonly=DEBUG,sql
#log4j.additivity.jdbc.sqlonly=false
log4j.logger.jdbc.sqltiming=DEBUG,sql
log4j.additivity.jdbc.sqltiming=false
log4j.logger.jdbc.resultset=INFO,sql
log4j.additivity.jdbc.resultset=false
log4j.logger.jdbc.audit=INFO,sql
log4j.additivity.jdbc.audit=false
log4j.logger.jdbc.connection=FATAL,sql
log4j.additivity.jdbc.connection=false
参考:
https://code.google.com/p/log4jdbc/
https://github.com/alewando/log4jdbc/blob/master/doc/log4j.properties