Druid内置提供了四种LogFilter(Log4jFilter、Log4j2Filter、CommonsLogFilter、Slf4jLogFilter),用于输出JDBC执行的日志。这些Filter都是Filter-Chain扩展机制中的Filter,所以配置方式可以参考这里:Filter配置
1. 别名映射
在druid-xxx.jar!/META-INF/druid-filter.properties文件中描述了这四种Filter的别名
druid.filters.log4j=com.alibaba.druid.filter.logging.Log4jFilter
druid.filters.log4j2=com.alibaba.druid.filter.logging.Log4j2Filter
druid.filters.slf4j=com.alibaba.druid.filter.logging.Slf4jLogFilter
druid.filters.commonlogging=com.alibaba.druid.filter.logging.CommonsLogFilter
druid.filters.commonLogging=com.alibaba.druid.filter.logging.CommonsLogFilter
他们的别名分别是log4j、log4j2、slf4j、commonlogging和commonLogging。其中commonlogging和commonLogging只是大小写不同。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
... ...
<property name="filters" value="stat,log4j" />
</bean>
2. loggerName配置
LogFilter都是缺省使用四种不同的Logger执行输出,看实现代码:
public abstract class LogFilter {
protected String dataSourceLoggerName = "druid.sql.DataSource";
protected String connectionLoggerName = "druid.sql.Connection";
protected String statementLoggerName = "druid.sql.Statement";
protected String resultSetLoggerName = "druid.sql.ResultSet";
}
你可以根据你的需要修改,在log4j.properties文件上做配置时,注意配置使用相关的logger。
2. 配置输出日志
缺省输入的日志信息全面,但是内容比较多,有时候我们需要定制化配置日志输出。
<bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
<property name="resultSetLogEnabled" value="false" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
...
<property name="proxyFilters">
<list>
<ref bean="log-filter"/>
</list>
</property>
</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文件配置日志输出选项,例如:
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout
5. 输出可执行的SQL
Java启动参数配置方式
-Ddruid.log.stmt.executableSql=true
logFilter参数直接配置
<bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
<property name="statementExecutableSqlLogEnable" value="true" />
</bean>
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter
时间: 2024-10-03 14:07:12