JPA使用log4jdbc輸出sql日志


前面兩篇介紹了JPA使用logback,log4j2輸出sql日志,雖然可以實現輸出Sql,但sql主體和參數都是分開輸出的,不方便調試,對開發不友好,我們還是喜歡直接把sql拿過來,直接就可以在plsql中運行,那就太爽了。

而log4jdbc就可以實現這個功能:

同樣使用上節的項目:我們再改一下POM:

增加:

<dependency>
            <groupId>com.googlecode.log4jdbc</groupId>
            <artifactId>log4jdbc</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>

yml配置文件修改:

server:
  port: 8086
spring:
  jpa:
    properties:
      hibernate:
        format_sql: true
    #配置在日志中打印出執行的 SQL 語句信息。
    #show-sql: true
  datasource:
   driver-class-name: net.sf.log4jdbc.DriverSpy
    url: jdbc:log4jdbc:mysql://***:3306/bmitest2?serverTimezone=Asia/Shanghai&useSSL=false&rewriteBatchedStatements=true&useCursorFetch=true
    username: root
    password: ***

logging:
  level:
    root: info

注意藍色部分的修改。

2020-05-08 15:15:07.284 [main] INFO  jdbc.audit - 1. Connection.isValid(5) returned true
2020-05-08 15:15:07.285 [main] INFO  jdbc.audit - 1. Connection.getAutoCommit() returned true
2020-05-08 15:15:07.285 [main] INFO  jdbc.audit - 1. Connection.setAutoCommit(false) returned 
2020-05-08 15:15:07.336 [main] INFO  jdbc.audit - 1. PreparedStatement.new PreparedStatement returned 
2020-05-08 15:15:07.337 [main] INFO  jdbc.audit - 1. Connection.prepareStatement(select opnfiledo0_.id as id1_0_0_ from opn_file opnfiledo0_ where opnfiledo0_.id=?) returned net.sf.log4jdbc.PreparedStatementSpy@39acd1f1
2020-05-08 15:15:07.340 [main] INFO  jdbc.audit - 1. PreparedStatement.setString(1, "14247") returned 
2020-05-08 15:15:07.342 [main] INFO  jdbc.sqlonly - select opnfiledo0_.id as id1_0_0_ from opn_file opnfiledo0_ where opnfiledo0_.id='14247' 

2020-05-08 15:15:07.347 [main] INFO  jdbc.sqltiming - select opnfiledo0_.id as id1_0_0_ from opn_file opnfiledo0_ where opnfiledo0_.id='14247' 
 {executed in 4 msec}
2020-05-08 15:15:07.352 [main] INFO  jdbc.resultset - 1. ResultSet.new ResultSet returned 
2020-05-08 15:15:07.353 [main] INFO  jdbc.audit - 1. PreparedStatement.executeQuery() returned net.sf.log4jdbc.ResultSetSpy@27e21083
2020-05-08 15:15:07.355 [main] INFO  jdbc.resultset - 1. ResultSet.next() returned true
2020-05-08 15:15:07.361 [main] INFO  jdbc.resultset - 1. ResultSet.next() returned false
2020-05-08 15:15:07.364 [main] INFO  jdbc.resultset - 1. ResultSet.close() returned 
2020-05-08 15:15:07.365 [main] INFO  jdbc.audit - 1. PreparedStatement.getMaxRows() returned 0
2020-05-08 15:15:07.365 [main] INFO  jdbc.audit - 1. PreparedStatement.getQueryTimeout() returned 0
2020-05-08 15:15:07.365 [main] INFO  jdbc.audit - 1. PreparedStatement.close() returned 
2020-05-08 15:15:07.373 [main] INFO  jdbc.audit - 1. Connection.commit() returned 
2020-05-08 15:15:07.374 [main] INFO  jdbc.audit - 1. Connection.setAutoCommit(true) returned 
2020-05-08 15:15:07.385 [main] INFO  jdbc.audit - 1. Connection.clearWarnings() returned 
2020-05-08 15:15:07.386 [main] INFO  jdbc.audit - 1. Connection.clearWarnings() returned 

修改后,我們可以看到sql了,但是多了很多我們不需要的日志,可以通過修改日志配置文件去掉,我們以log4j2為例:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>

        <!--這個輸出控制台的配置-->
        <console name="Console" target="SYSTEM_OUT">
            <!--輸出日志的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t{2}] %-5level %logger{4} - %msg%n"/>
        </console>
        <!-- 這個會打印出所有的info及以下級別的信息,每次大小超過size,則這size大小的日志會自動存入按年份-月份建立的文件夾下面並進行壓縮,作為存檔-->
        <RollingFile name="RollingFileInfo" fileName="logs/cis-mr-audit.log"
                     filePattern="logs/$${date:yyyy-MM}/cis-mr-audit-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="5 MB"/>
            </Policies>
        </RollingFile>

    </Appenders>
    <Loggers>
        <logger name="jdbc.audit" level="OFF"/>
        <logger name="jdbc.resultset" level="OFF"/>
        <logger name="jdbc.connection" level="OFF"/>
        <logger name="jdbc.sqltiming" level="OFF"/>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="RollingFileInfo" />
        </Root>
    </Loggers>
</Configuration>
2020-05-08 15:20:55.334 [main] INFO  apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-auto-1"]
2020-05-08 15:20:55.357 [main] INFO  web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 61870 (http) with context path ''
2020-05-08 15:20:55.358 [main] INFO  com.example.demo.FileControllerTest - Started FileControllerTest in 4.543 seconds (JVM running for 7.048)
2020-05-08 15:20:55.413 [main] INFO  ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring TestDispatcherServlet ''
2020-05-08 15:20:55.413 [main] INFO  test.web.servlet.TestDispatcherServlet - Initializing Servlet ''
2020-05-08 15:20:55.428 [main] INFO  test.web.servlet.TestDispatcherServlet - Completed initialization in 14 ms
2020-05-08 15:20:55.548 [main] INFO  jdbc.sqlonly - select opnfiledo0_.id as id1_0_0_ from opn_file opnfiledo0_ where opnfiledo0_.id='14247' 

日志輸出就正常了。

調整一下日志級別為error:

logging:
  level:
    root: error

沒事sql日志輸出。

這個方法更為簡單實用。推薦使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM