logback + log4jdbc-log4j2 输出 jpa sql 参数实际value

来源:互联网 发布:js options click事件 编辑:程序博客网 时间:2024/06/16 20:21


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 2. SLF4J 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  |admin@cs.com |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"/>

完成。










0 0