Hibernate log4jdbc

log4jdbc用于打印于SQL 同时打出绑定参数的值:

log4jdbc:   http://code.google.com/p/log4jdbc/
另外一个对log4jdbc的扩展:  http://code.google.com/p/log4jdbc-remix/
在官网首页已经将用法讲的很清楚了
JDBC 4 support was added with the JDK 1.6 release and adds many additional features over and above JDBC 3. However, the log4jdbc JDBC 4 driver can wrap a JDBC 3 or older driver and it's recommended that if you use JDK 1.6 or above, that you use the log4jdbc JDBC 4 driver that is compiled with JDK 1.6.
    log4jdbc3-1.2.jar for JDBC 3 support in JDK 1.4 , JDK 1.5
    log4jdbc4-1.2.jar for JDBC 4 support in JDK 1.6 , JDK 1.7

Set your JDBC driver class to net.sf.log4jdbc.DriverSpy in your application's configuration.
Driver Class                         Database Type
oracle.jdbc.driver.OracleDriver     Older Oracle Driver
oracle.jdbc.OracleDriver             Newer Oracle Driver
com.sybase.jdbc2.jdbc.SybDriver     Sybase
net.sourceforge.jtds.jdbc.Driver     jTDS SQL Server & Sybase driver
com.microsoft.jdbc.sqlserver.SQLServerDriver     Microsoft SQL Server 2000 driver
com.microsoft.sqlserver.jdbc.SQLServerDriver     Microsoft SQL Server 2005 driver
weblogic.jdbc.sqlserver.SQLServerDriver         Weblogic SQL Server driver
com.informix.jdbc.IfxDriver                     Informix
org.apache.derby.jdbc.ClientDriver                 Apache Derby client/server driver, aka the Java DB
org.apache.derby.jdbc.EmbeddedDriver             Apache Derby embedded driver, aka the Java DB
com.mysql.jdbc.Driver                            MySQL
org.postgresql.Driver                             PostgresSQL
org.hsqldb.jdbcDriver                             HSQLDB pure Java database
org.h2.Driver                                     H2 pure Java database

Prepend jdbc:log4 to the normal jdbc url that you are using.
    jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase改为:
    jdbc:log4jdbc:derby://localhost:1527//db-derby-10.2.2.0-bin/databases/MyDatabase
   
Set up your loggers.
    Each of these logs can be set at either DEBUG, INFO or ERROR level.
    DEBUG includes the class name and line number (if available) at which the SQL was executed. Use DEBUG level with extra care, as this imposes an additional performance penalty when in use.
    INFO includes the SQL (or other information as applicable.)
    ERROR will show the stack traces in the log output when SQLExceptions occur.

    jdbc.sqlonly     Logs only SQL. SQL executed within a prepared statement is automatically shown with it's bind arguments replaced with the data bound at that position, for greatly increased readability.     1.0
    jdbc.sqltiming     Logs the SQL, post-execution, including timing statistics on how long the SQL took to execute.     1.0
    jdbc.audit     Logs ALL JDBC calls except for ResultSets. This is a very voluminous output, and is not normally needed unless tracking down a specific JDBC problem.     1.0
    jdbc.resultset     Even more voluminous, because all calls to ResultSet objects are logged.     1.0
    jdbc.connection     Logs connection open and close events as well as dumping all open connection numbers. This is very useful for hunting down connection leak problems.     1.2alpha1

 

使用很简单,将JDBC URL修改,同时修改驱动即可
<property name="connection.url">jdbc:log4jdbc:h2:tcp://localhost/~/dbname</property>
<property name="connection.driver_class">net.sf.log4jdbc.DriverSpy</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>

 

log4j中配置日志的输出

log4j.logger.jdbc.sqlonly=OFF 
log4j.logger.jdbc.sqltiming=INFO 
log4j.logger.jdbc.audit=OFF 
log4j.logger.jdbc.resultset=OFF 
log4j.logger.jdbc.connection=OFF 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值