Druid 配置_LogFilter


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM