About springboot configuration p6spy log plugin

Recently, the new project of the project team switched to the springboot framework. When the framework was built, the SQL log was not printed. I searched for the p6spy related records of springboot configuration in Du Niang, and found nothing.  
So I thought of the p6spy configuration used for SQL formatted output when the springmvc framework was used in the previous project.
<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
 <constructor-arg ref="dataSource_org"></constructor-arg>
</bean>
<bean id="dataSource_org" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${jdbc.driver.mysql}"/>
  <property name="jdbcUrl" value="${jdbc.url}" />
  <property name="user" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
  <property name="initialPoolSize" value="10" />
  <property name="maxPoolSize" value="200" />
  <property name="maxIdleTime" value="6000" />
  <property name="idleConnectionTestPeriod" value="600"/>
  <property name="preferredTestQuery" value="SELECT 1"/>
</bean>

After reference, because springboot omits the configuration of the xml file, when the java file configures the data connection,
use the annotation to declare the bean object
@Bean
@Primary
public DataSource spyDataSource(){
    return new P6DataSource(druidDataSource());
}
Replace the above code
<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
 <constructor-arg ref="dataSource_org"></constructor-arg>
</bean>

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;
	}

After configuration, the sql log will be printed to the spy.log file under the project by default.
If you need to change the log directory
, delete the # sign before logfile = log/spy.log in the configuration file spy.properties and configure log printing. path

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361306&siteId=291194637