javaweb log4j显示完整sql日志

javaweb显示完整sql日志

所需jar包:

log4j-1.2.17.jar
log4jdbc-1.2.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar

  1. jar包添加完成后,添加log4j.properties
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%n%d - %p - [%c]%n%m%n
 #log4jdbc 只输出sqltiming,其他关闭
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
  1. 以上配置完成后,还不能打印出完整sql。再配置一下数据库连接
    以mysql为例,其他数据库同理
#driverClass=com.mysql.jdbc.Driver 改为
driverClass=net.sf.log4jdbc.DriverSpy
#jdbcUrl=jdbc:mysql://localhost:3306/test 改为
jdbcUrl=jdbc:log4jdbc:mysql://localhost:3306/test
  1. maven
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>
<dependency>
  <groupId>com.googlecode.log4jdbc</groupId>
  <artifactId>log4jdbc</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.12</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.12</version>
</dependency>
  1. 结果
2016-03-04 13:40:37,844 - INFO - [jdbc.sqltiming]
SELECT ID,NAME,PASSWORD,EMAIL FROM USER WHERE ID = 1 
 {executed in 2 msec}
posted @ 2016-03-07 11:32  孙行者、  阅读(1234)  评论(0编辑  收藏  举报