初探Druid连接池之二——慢SQL日志记录

由于工作原因,这里先不说连接数据库部分的配置,后面会补上,直接进入慢SQL日志记录。

 

1.applicationContext.xml中增加如下配置:

<bean abstract="true" id="mysql_database" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 
        <!-- 监控统计拦截的filters,日志记录 edited by zhangpl 20140824 start -->
	<!-- 并在filters属性中配置了log4j -->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter" />
                <ref bean="log-filter" />
            </list> 
        </property>
	 <!-- 监控统计拦截的filters,日志记录 edited by zhangpl 20140824 end -->
        <property name="filters">
	    <value>stat,wall,log4j</value>
	</property>

        <!-- 其它基本配置略...-->
</bean>

<!-- 慢SQL记录  edited by zhangpl 20140824  start-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <property name="mergeSql" value="true" />
        <property name="slowSqlMillis" value="1000" />
        <property name="logSlowSql" value="true" />
</bean>
    
<bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
        <!-- <property name="resultSetLogEnabled" value="false" /> -->
        <!-- <property name="statementExecutableSqlLogEnable" value="true" /> -->
</bean>
<!-- 慢SQL记录  edited by zhangpl 20140824  end-->

 

 

2.log4j.properties中配置如下:

log4j.rootLogger=warn,A1,A2

#1.打印druid连接池SQL语句,info级别的语句打印到info_slowsql.log,warn级别语句打印到warn_slowlog.log;
#2.将日志写到文件中; 

# Druid
log4j.logger.druid.sql=info,A1,A2
log4j.logger.druid.sql.DataSource=info,A1,A2
log4j.logger.druid.sql.Connection=info,A1,A2
log4j.logger.druid.sql.Statement=info,A1,A2
log4j.logger.druid.sql.ResultSet=info,A1,A2

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=../druid-logs/info_slowsql.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
#log4j.appender.A1.File=${catalina.home}/logs/info_slowsql.log
#log4j.appender.A1.File=${WebApp.TomcatHome}/logs/info_slowsql.log
log4j.appender.A1.ImmediateFlush=true
log4j.appender.A1.Append=true
log4j.appender.A1.Threshold=info
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] - %m%n

log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File=../druid-logs/warn_slowsql.log
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
#log4j.appender.A2.File=${catalina.home}/logs/warn_slowsql.log
#log4j.appender.A2.File=${WebApp.TomcatHome}/logs/warn_slowsql.log
log4j.appender.A2.ImmediateFlush=true
log4j.appender.A2.Append=true
log4j.appender.A2.Threshold=warn
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] - %m%n

 

 经过上面的配置,就会在文件中打印,如下的慢SQL语句:

......
[2014-08-26 11:12:59] [ERROR] [com.alibaba.druid.filter.stat.StatFilter] - slow sql 3121 millis. 
select count(1) from person where schoolid=? and isDeleted=0   and GET_FIRST_PINYIN_CHAR(username) =?  
["145","a"]
......

 

有问题欢迎指正,可参考官方文档,以官方文档为准。

 

参考:http://www.oschina.net/question/97503_117693

你可能感兴趣的:(日志,连接池,druid,慢SQL)