Druid中有DruidDataSource/Spring/Web等监控记录,其中DruidDataSource提供了保存监控记录的API。

保存DruidDataSource的监控记录

DruidDataSource有一个属性timeBetweenLogStatsMillis,配置timeBetweenLogStatsMillis>0之后,DruidDataSource会定期把监控数据输出到日志中。

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

或者通过jvm启动参数来指定,例如:

  1. -Ddruid.timeBetweenLogStatsMillis=300000

定制StatLogger

DruidDataSource是通过com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl.DruidDataSourceStatLoggerImpl来实现输入监控数据到日志的,你可以自定义一个StatLogger,例如:

Java代码

  1. import com.alibaba.druid.pool.DruidDataSourceStatLoggerAdapter;
  2. import com.alibaba.druid.pool.DruidDataSourceStatLogger ;
  3. public class MyStatLogger extends DruidDataSourceStatLoggerAdapter implements DruidDataSourceStatLogger {
  4. ... ...
  5. }

配置

  1. <bean id="myStatLogger" class="com.mycompany.MyStatLogger">
  2. ... ...
  3. </bean>
  4. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  5. ... ...
  6. <property name="statLogger" ref="myStatLogger" />
  7. ... ...
  8. </bean>