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