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

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

官網:sql

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

 

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:api

  • 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)

效果:app

 

[sql] view plain copymaven

  1. 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   
  2. as name4_2_, user0_.password as password5_2_, user0_.salt as salt6_2_, user0_.status as status7_2_,   
  3. user0_.team_id as team_id8_2_ from ss_user user0_ where user0_.login_name='admin'   
  4.  {executed in 1 msec}  
  5. 17:46:44.383 [qtp79368068-124] INFO  jdbc.resultsettable -   
  6. |---|-------------|-----------|--------------|-----------------------------------------|-----------------|--------|--------|  
  7. |id |email        |login_name |name          |password                                 |salt             |status  |team_id |  
  8. |---|-------------|-----------|--------------|-----------------------------------------|-----------------|--------|--------|  
  9. |1  |admin@cs.com |admin      |administrator |692235df64f14215b1f15ccd79b70b55e63940fa |7awe2safd3b3asdd |enabled |1       |  
  10. |---|-------------|-----------|--------------|-----------------------------------------|-----------------|--------|--------|  


 

 

 

配置:ide

首先引入依賴jar:優化

 

[html] view plain copygoogle

  1. <dependency>  
  2.   <groupId>org.bgee.log4jdbc-log4j2</groupId>  
  3.   <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>  
  4.   <version>1.16</version>  
  5. </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=-1url

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文件:

 

[html] view plain copy

  1. <!--log4jdbc -->  
  2. <!--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.-->  
  3. <logger name="jdbc.sqlonly" level="WARN"/>  
  4. <logger name="jdbc.sqltiming" level="INFO"/>  
  5. <logger name="jdbc.resultsettable" level="INFO"/>  
  6. <logger name="jdbc.resultset" level="WARN"/>  
  7. <logger name="jdbc.connection" level="WARN"/>  
  8. <logger name="jdbc.audit" level="WARN"/>  

 

[html] view plain copy

  1.   

 

完成。

相關文章
相關標籤/搜索