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

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

 

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


 
        
	
        
            
                
                
             
        
	 
        
	    stat,wall,log4j
	

        




        
        
        

    

        
        

 

 

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)