1、maven安装依赖
<!-- logger --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency>
2、jdbc.properties的配置
jdbc.driverClassName=net.sf.log4jdbc.DriverSpyjdbc.url=jdbc:log4jdbc:mysql://localhost:3306/xdptest?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true jdbc.username=root jdbc.password=root
3、spring中的配置:
<bean id="dataSourceSpied" class="..."> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> ... </bean> <!-- 注意:其它引用(如事务管理配置),用dataSource,而不是dataSourceSpied --> <bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource"> <constructor-arg ref="dataSourceSpied" /> </bean>
也可以格式化一下sql输出,配置如下:
<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource"> <constructor-arg ref="dataSourceSpied" /> <property name="logFormatter"> <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter"> <property name="loggingType" value="MULTI_LINE" /> <property name="margin" value="19" /> <property name="sqlPrefix" value="SQL:::" /> </bean> </property> </bean>
4、log4j.properties配置:
##-------------log4jdbc 配置方式示例---------------## #值设置方式 :如果关闭设置为OFF,如果开启设置为ON(默认debug级别)或(设置输出级别,输出器) log4j.logger.jdbc.sqlonly=OFF log4j.logger.jdbc.sqltiming=INFO,sql log4j.logger.jdbc.audit=OFF log4j.logger.jdbc.resultset=ON log4j.logger.jdbc.connection=OFF log4j.logger.jdbc.resultsettable=INFO,sql log4j.additivity.jdbc.sqlonly=false log4j.additivity.jdbc.sqltiming=false log4j.additivity.jdbc.audidt=false log4j.additivity.jdbc.resultset=false log4j.additivity.jdbc.connection=false log4j.additivity.jdbc.resultsettable=false ! the appender used for the JDBC API layer call logging above, sql only log4j.appender.sql=org.apache.log4j.FileAppender log4j.appender.sql.File=D:/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} %m%n%n log4j.additivity.sql=false
5、参考文章
log4jdbc-remix官网:https://code.google.com/p/log4jdbc-remix/
http://houfeng0923.iteye.com/blog/1266375
时间: 2024-11-03 22:24:10