Spring Boot 中使用log4jdbc记录SQL的运行时参数
- 关于log4jdbc
log4jdbc is a Java JDBC driver that can log SQL and/or JDBC calls (and optionally SQL timing information) for other JDBC drivers using the Simple Logging Facade For Java (SLF4J) logging system.
在github上可以找到log4jdbc git的代码。
- 项目文件 结构
- 在appliaction.properties中添加必要的配置
spring.datasource.url = jdbc:log4jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=UTF-8 # Username and password spring.datasource.username = test spring.datasource.password = **** spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy # =============================== # = JPA / HIBERNATE # =============================== # Use spring.jpa.properties.* for Hibernate native properties (the prefix is # stripped before adding them to the entity manager). # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update): with "update" the database # schema will be automatically updated accordingly to java entities found in # the project spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # Allows Hibernate to generate SQL optimized for a particular DBMS spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
- 添加logback配置就文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="jdbc.connection" additivity="false" level="FATAL">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<logger name="jdbc.resultset" additivity="false" level="INFO"