logback + log4jdbc-log4j2 輸出 jpa sql 參數實際value


log4jdbc-log4j2是基於log4jdbc開發,是log4jdbc的一個變體。 在result輸入格式上做了非常完美的優化。

官網:

https://code.google.com/p/log4jdbc-log4j2/

log4jdbc-log4j2 is a modification of log4jdbc to natively use Log4j 2 (or SLF4J as usual), that supports JDBC 4.1 to JDBC 3, includes all the improvements of log4jdbc-remix, and provides new improvements on its own. log4jdbc-log4j2:

  • natively supports Log4j 2SLF4J can still be used as usual.
  • supports JDBC 4.1 (Java 7), JDBC 4 (Java 6), JDBC 3 (Java 5).
  • includes all the improvements of log4jdbc-remix (can log result sets as tables, can be configured as a Datasource, can use a plugable SQL formatter).
  • is available in the sonatype maven repository.
  • provides new improvements on its own (logging of connection opening execution time, of getGeneratedKeys() queries, etc)
效果:

17:46:44.381 [qtp79368068-124] INFO  jdbc.sqltiming - select user0_.id as id1_2_, user0_.email as email2_2_, user0_.login_name as login_na3_2_, user0_.name 
as name4_2_, user0_.password as password5_2_, user0_.salt as salt6_2_, user0_.status as status7_2_, 
user0_.team_id as team_id8_2_ from ss_user user0_ where user0_.login_name='admin' 
 {executed in 1 msec}
17:46:44.383 [qtp79368068-124] INFO  jdbc.resultsettable - 
|---|-------------|-----------|--------------|-----------------------------------------|-----------------|--------|--------|
|id |email        |login_name |name          |password                                 |salt             |status  |team_id |
|---|-------------|-----------|--------------|-----------------------------------------|-----------------|--------|--------|
|1  |[email protected] |admin      |administrator |692235df64f14215b1f15ccd79b70b55e63940fa |7awe2safd3b3asdd |enabled |1       |
|---|-------------|-----------|--------------|-----------------------------------------|-----------------|--------|--------|



配置:

首先引入依賴jar:

      <dependency>
        <groupId>org.bgee.log4jdbc-log4j2</groupId>
        <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
        <version>1.16</version>
      </dependency>


1. 設置jdbc driver, 在db.properties中加上:
#log4jdbc-Log4j2 driver
jdbc.driver=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
jdbc.url=jdbc:log4jdbc:h2:file:~/.h2/sswxy_db;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1

2. 在classpath下新增文件 log4jdbc.log4j2.properties ,內容:

# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

3. logback.xml文件:

    <!--log4jdbc -->
    <!--In a typical usage scenario, you might turn on only the jdbc.sqlonly logging at INFO level, just to view the SQL coming out of your program.-->
    <logger name="jdbc.sqlonly" level="WARN"/>
    <logger name="jdbc.sqltiming" level="INFO"/>
    <logger name="jdbc.resultsettable" level="INFO"/>
    <logger name="jdbc.resultset" level="WARN"/>
    <logger name="jdbc.connection" level="WARN"/>
    <logger name="jdbc.audit" level="WARN"/>


完成。










發佈了36 篇原創文章 · 獲贊 12 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章