一、maven安裝依賴 java
<dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency>
二、spring中的配置: spring
<bean id="dataSourceSpied" class="..."> <property name="driverClass" value="${datasource.driverClassName}"/> <property name="jdbcUrl" value="${datasource.url}"/> <property name="user" value="${datasource.username}"/> <property name="password" value="${datasource.password}"/> ... </bean> <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>
三、log4j.properties配置 sql
##-------------log4jdbc 配置方式示例---------------## #值設置方式 :若是關閉設置爲OFF,若是開啓設置爲ON(默認debug級別)或(設置輸出級別,輸出器) log4j.logger.jdbc.sqlonly=OFF log4j.logger.jdbc.sqltiming=INFO,sql log4j.logger.jdbc.audit=OFF log4j.logger.jdbc.resultset=OFF 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
四、參考文章 apache
log4jdbc-remix官網:https://code.google.com/p/log4jdbc-remix/ app
http://houfeng0923.iteye.com/blog/1266375 maven