关于springboot配置p6spy日志插件

最近项目组新项目转用springboot框架,框架搭建时,未打印SQL日志。在度娘中查找springboot配置p6spy相关记录,查无所获 :cry: :cry:
于是想到之前项目用springmvc框架时,SQL格式化输出用的p6spy配置。
















借鉴之后,因为springboot省略了xml文件的配置,所以java文件配置数据连接时,
使用注解声明bean对象

@Bean
@Primary
public DataSource spyDataSource(){
return new P6DataSource(druidDataSource());
}
以上代码代替




public DataSource druidDataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
datasource.setConnectionProperties(connectionProperties);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
return datasource;
}

配置后,sql日志默认打印到项目下spy.log文件里
若要需改日志目录需要,将配置文件spy.properties中logfile = log/spy.log
logfile前的#号删掉 等号后配置日志打印路径

你可能感兴趣的:(springboot)