0%

Druid-Demo

阅读更多

1 Slf4jLogFilter

Slf4jLogFilter的日志级别都是DEBUG的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Bean
public Slf4jLogFilter logFilter() {
Slf4jLogFilter filter = new Slf4jLogFilter();
filter.setDataSourceLogger(LOGGER);
filter.setStatementLogger(LOGGER);
filter.setConnectionLogger(LOGGER);
filter.setResultSetLogger(LOGGER);

// filter.setStatementLogEnabled(false);
// filter.setStatementParameterClearLogEnable(false);
// filter.setStatementLogErrorEnabled(false);
// filter.setStatementCreateAfterLogEnabled(false);
// filter.setStatementCloseAfterLogEnabled(false);
// filter.setStatementPrepareCallAfterLogEnabled(false);
// filter.setStatementParameterClearLogEnable(false);
// filter.setStatementParameterSetLogEnabled(false);
// filter.setStatementExecuteAfterLogEnabled(false);
// filter.setStatementExecuteBatchAfterLogEnabled(false);
// filter.setStatementExecuteUpdateAfterLogEnabled(false);
// filter.setStatementExecutableSqlLogEnable(true);
// filter.setStatementExecuteQueryAfterLogEnabled(false);
//
// filter.setConnectionLogEnabled(false);
// filter.setConnectionLogErrorEnabled(false);
// filter.setConnectionConnectBeforeLogEnabled(false);
// filter.setConnectionCloseAfterLogEnabled(false);
// filter.setConnectionCommitAfterLogEnabled(false);
// filter.setConnectionConnectAfterLogEnabled(false);
// filter.setConnectionRollbackAfterLogEnabled(false);
//
// filter.setDataSourceLogEnabled(false);
//
// filter.setResultSetLogEnabled(false);
// filter.setResultSetOpenAfterLogEnabled(false);
// filter.setResultSetLogErrorEnabled(false);
// filter.setResultSetCloseAfterLogEnabled(false);
// filter.setResultSetNextAfterLogEnabled(false);

return filter;
}

2 StatFilter

StatFilter 与 Slf4jLogFilter 无关,StatFilter有自己的Logger,即

1
2
3
4
public class StatFilter extends FilterEventAdapter implements StatFilterMBean {

private final static Log LOG = LogFactory.getLog(StatFilter.class);
}

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Bean
public StatFilter statFilter() {
StatFilter statFilter = new StatFilter();
statFilter.setLogSlowSql(true);
statFilter.setSlowSqlMillis(50);
statFilter.setMergeSql(true);
return statFilter;
}

@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("liuye");
dataSource.setPassword("123456");
dataSource.setMaxActive(50);
dataSource.setMaxWait(3000);
dataSource.setProxyFilters(Collections.singletonList(statFilter()));
return dataSource;
}

3 参考