Druid内置提供了四种LogFilter(Log4jFilter、Log4j2Filter、CommonsLogFilter、Slf4jLogFilter),用于输出JDBC执行的日志。这些Filter都是Filter-Chain扩展机制中的Filter,所以配置方式可以参考这里:Filter配置

1. 别名映射

在druid-xxx.jar!/META-INF/druid-filter.properties文件中描述了这四种Filter的别名

  1. druid.filters.log4j=com.alibaba.druid.filter.logging.Log4jFilter
  2. druid.filters.log4j2=com.alibaba.druid.filter.logging.Log4j2Filter
  3. druid.filters.slf4j=com.alibaba.druid.filter.logging.Slf4jLogFilter
  4. druid.filters.commonlogging=com.alibaba.druid.filter.logging.CommonsLogFilter
  5. druid.filters.commonLogging=com.alibaba.druid.filter.logging.CommonsLogFilter

他们的别名分别是log4j、log4j2、slf4j、commonlogging和commonLogging。其中commonlogging和commonLogging只是大小写不同。

  1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  2. init-method="init" destroy-method="close">
  3. ... ...
  4. <property name="filters" value="stat,log4j" />
  5. </bean>

2. loggerName配置

LogFilter都是缺省使用四种不同的Logger执行输出,看实现代码:

  1. public abstract class LogFilter {
  2. protected String dataSourceLoggerName = "druid.sql.DataSource";
  3. protected String connectionLoggerName = "druid.sql.Connection";
  4. protected String statementLoggerName = "druid.sql.Statement";
  5. protected String resultSetLoggerName = "druid.sql.ResultSet";
  6. }

你可以根据你的需要修改,在log4j.properties文件上做配置时,注意配置使用相关的logger。

3. 配置输出日志

缺省输入的日志信息全面,但是内容比较多,有时候我们需要定制化配置日志输出。

  1. <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
  2. <property name="resultSetLogEnabled" value="false" />
  3. </bean>
  4. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  5. ...
  6. <property name="proxyFilters">
  7. <list>
  8. <ref bean="log-filter"/>
  9. </list>
  10. </property>
  11. </bean>
参数说明
dataSourceLogEnabled所有DataSource相关的日志
connectionLogEnabled所有连接相关的日志
connectionLogErrorEnabled所有连接上发生异常的日志
statementLogEnabled所有Statement相关的日志
statementLogErrorEnabled所有Statement发生异常的日志
resultSetLogEnabled
resultSetLogErrorEnabled
connectionConnectBeforeLogEnabled
connectionConnectAfterLogEnabled
connectionCommitAfterLogEnabled
connectionRollbackAfterLogEnabled
connectionCloseAfterLogEnabled
statementCreateAfterLogEnabled
statementPrepareAfterLogEnabled
statementPrepareCallAfterLogEnabled
statementExecuteAfterLogEnabled
statementExecuteQueryAfterLogEnabled
statementExecuteUpdateAfterLogEnabled
statementExecuteBatchAfterLogEnabled
statementCloseAfterLogEnabled
statementParameterSetLogEnabled
resultSetNextAfterLogEnabled
resultSetOpenAfterLogEnabled
resultSetCloseAfterLogEnabled

4. log4j.properties配置

如果你使用log4j,可以通过log4j.properties文件配置日志输出选项,例如:

  1. log4j.logger.druid.sql=warn,stdout
  2. log4j.logger.druid.sql.DataSource=warn,stdout
  3. log4j.logger.druid.sql.Connection=warn,stdout
  4. log4j.logger.druid.sql.Statement=warn,stdout
  5. log4j.logger.druid.sql.ResultSet=warn,stdout

5. 输出可执行的SQL

Java启动参数配置方式

  1. -Ddruid.log.stmt.executableSql=true

logFilter参数直接配置

  1. <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
  2. <property name="statementExecutableSqlLogEnable" value="true" />
  3. </bean>