启用休眠日志记录 [英] Enable Hibernate logging

查看:46
本文介绍了启用休眠日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建休眠语句的日志.我使用 JPA 执行我的 sql 语句,其中 Hibernate 2.0 是持久性提供程序(我的应用程序服务器是 JBoss AS 6.0).我使用 EJB 3.0 提供的 EntityManager 接口调用我的 CRUD 方法.我阅读了许多关于启用 hinernate 日志记录的帖子,但实际上我看不到任何日志:-(我创建了一个 log4j.properties 文件,并将它放在我的 Netbeans 项目的根文件夹中.我还在项目的类路径中放置了 log4j 库.我的 log4j.properties 如下:

I'm trying to create a log of hibernate statements. I perform my sql statements using JPA where Hibernate 2.0 is the persistence provider (my application server is JBoss AS 6.0). I call my CRUD methods using the EntityManager interface provided by EJB 3.0. I read many posts about enabling hinernate logging but actually i can't see any log :-( I create a log4j.properties file and I put it in the root folder of my Netbeans project. I put also log4j library in the classpath of the project. My log4j.properties s the following:

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n


log4j.rootLogger=debug, stdout

log4j.logger.org.hibernate=info

 ### log just the SQL
log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=info

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
log4j.logger.org.hibernate.cache=info

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

为什么我看不到日志信息?我想在 sql where 子句或 insert 语句中看到值(实际上我只看到一些?")提前致谢.

Why can't I see the log informations? I would like see values in sql where clauses or in insert statements (actually I see only some '?') Thanks in advance.

为了清楚起见,我将项目的文件夹结构放在下面:

For clarity I put below the folder structure of my project:

MyProject
¦   build.xml
¦   log4j.properties
¦
+---build
¦   ¦   capitolo2-ejb.jar
¦   ¦   capitolo2-war.war
¦   ¦   JBoss4.dpf
¦   ¦
¦   +---lib
¦   ¦       log4j-1.2.15.jar
¦   ¦       slf4j-api.jar
¦   ¦       slf4j-jboss-logmanager.jar
¦   ¦
¦   +---META-INF
¦           chapter2-hornetq-jms.xml
¦           jboss-app.xml
¦           log4j.properties
¦           MANIFEST.MF
¦
+---capitolo2-ejb
¦   ¦   build.xml
¦   ¦   
¦   ¦
¦   +---build
¦   ¦   +---classes
¦   ¦   ¦   ¦   .netbeans_automatic_build
¦   ¦   ¦   ¦   .netbeans_update_resources
¦   ¦   ¦   ¦
¦   ¦   ¦   +---ejb
¦   ¦   ¦   ¦   +---com
¦   ¦   ¦   ¦       +---ejb3inaction
¦   ¦   ¦   ¦           +---actionbazaar
¦   ¦   ¦   ¦               +---buslogic
¦   ¦   ¦   ¦               ¦       BillingException.class
¦   ¦   ¦   ¦               ¦       OrderBillingMDB.class
¦   ¦   ¦   ¦               ¦       PlaceBid.class
¦   ¦   ¦   ¦               ¦       PlaceBidBean.class
¦   ¦   ¦   ¦               ¦       PlaceOrder.class
¦   ¦   ¦   ¦               ¦       PlaceOrderBean.class
¦   ¦   ¦   ¦               ¦
¦   ¦   ¦   ¦               +---persistence
¦   ¦   ¦   ¦                       Bid.class
¦   ¦   ¦   ¦                       BillingInfo.class
¦   ¦   ¦   ¦                       Order.class
¦   ¦   ¦   ¦                       OrderStatus.class
¦   ¦   ¦   ¦                       ShippingInfo.class
¦   ¦   ¦   ¦
¦   ¦   ¦   +---META-INF
¦   ¦   ¦           beans.xml
¦   ¦   ¦           jboss.xml
¦   ¦   ¦           MANIFEST.MF
¦   ¦   ¦           persistence.xml
¦   ¦   ¦
¦   ¦   +---empty
¦   ¦   +---generated-sources
¦   ¦       +---ap-source-output
¦   +---dist
¦   ¦       capitolo2-ejb.jar
¦   ¦
¦   +---lib
¦   ¦       log4j-1.2.15.jar
¦   ¦
¦   +---nbproject
¦   ¦   ¦   ant-deploy.xml
¦   ¦   ¦   build-impl.xml
¦   ¦   ¦   genfiles.properties
¦   ¦   ¦   project.properties
¦   ¦   ¦   project.xml
¦   ¦   ¦
¦   ¦   +---private
¦   ¦           private.properties
¦   ¦           private.xml
¦   ¦
¦   +---setup
¦   ¦       jboss-ds.xml
¦   ¦
¦   +---src
¦       +---conf
¦       ¦       beans.xml
¦       ¦       jboss.xml
¦       ¦       MANIFEST.MF
¦       ¦       persistence.xml
¦       ¦
¦       +---java
¦           +---ejb
¦               +---com
¦                   +---ejb3inaction
¦                       +---actionbazaar
¦                           +---buslogic
¦                           ¦       BillingException.java
¦                           ¦       OrderBillingMDB.java
¦                           ¦       PlaceBid.java
¦                           ¦       PlaceBidBean.java
¦                           ¦       PlaceOrder.java
¦                           ¦       PlaceOrderBean.java
¦                           ¦
¦                           +---persistence
¦                                   Bid.java
¦                                   BillingInfo.java
¦                                   Order.java
¦                                   OrderStatus.java
¦                                   ShippingInfo.java
¦
+---capitolo2-war
¦   ¦   build.xml
¦   ¦
¦   +---build
¦   ¦   +---empty
¦   ¦   +---generated-sources
¦   ¦   ¦   +---ap-source-output
¦   ¦   +---lib
¦   ¦   ¦       log4j-1.2.15.jar
¦   ¦   ¦       slf4j-api.jar
¦   ¦   ¦       slf4j-jboss-logmanager.jar
¦   ¦   ¦
¦   ¦   +---web
¦   ¦       ¦   index.jsp
¦   ¦       ¦
¦   ¦       +---META-INF
¦   ¦       ¦       MANIFEST.MF
¦   ¦       ¦
¦   ¦       +---WEB-INF
¦   ¦           ¦   beans.xml
¦   ¦           ¦   jboss-web.xml
¦   ¦           ¦
¦   ¦           +---classes
¦   ¦               ¦   .netbeans_automatic_build
¦   ¦               ¦   .netbeans_update_resources
¦   ¦               ¦
¦   ¦               +---it
¦   ¦                   +---myservlets
¦   ¦                           PlaceBidServlet.class
¦   ¦                           PlaceOrderServlet.class
¦   ¦
¦   +---dist
¦   ¦       capitolo2-war.war
¦   ¦
¦   +---nbproject
¦   ¦   ¦   ant-deploy.xml
¦   ¦   ¦   build-impl.xml
¦   ¦   ¦   genfiles.properties
¦   ¦   ¦   project.properties
¦   ¦   ¦   project.xml
¦   ¦   ¦
¦   ¦   +---private
¦   ¦           private.properties
¦   ¦           private.xml
¦   ¦
¦   +---setup
¦   ¦       jboss-ds.xml
¦   ¦
¦   +---src
¦   ¦   +---conf
¦   ¦   ¦       MANIFEST.MF
¦   ¦   ¦
¦   ¦   +---java
¦   ¦       +---it
¦   ¦           +---myservlets
¦   ¦                   PlaceBidServlet.java
¦   ¦                   PlaceOrderServlet.java
¦   ¦
¦   +---web
¦       ¦   index.jsp
¦       ¦
¦       +---WEB-INF
¦               beans.xml
¦               jboss-web.xml
¦
+---dist
¦       capitolo2.ear
¦
+---nbproject
¦   ¦   ant-deploy.xml
¦   ¦   build-impl.xml
¦   ¦   genfiles.properties
¦   ¦   project.properties
¦   ¦   project.xml
¦   ¦
¦   +---private
¦           private.properties
¦
+---setup
¦       jboss-ds.xml
¦       jboss4-netbeans-destinations-service.xml
¦       log4j.properties
¦
+---src
    +---conf
            chapter2-hornetq-jms.xml
            jboss-app.xml
            log4j.properties
            MANIFEST.MF

推荐答案

还必须在休眠配置中启用休眠日志记录.

Hibernate logging has to be also enabled in hibernate configuration.

添加行

hibernate.show_sql=true
hibernate.format_sql=true

要么

serverdefaultdeployersejb3.deployerMETA-INFjpa-deployers-jboss-beans.xml

标签中的应用程序persistence.xml.

无论如何,休眠日志记录不会包含(以有用的形式)有关实际准备好的语句参数的信息.

Anyway hibernate logging won't include (in useful form) info on actual prepared statements' parameters.

还有一种使用 log4jdbc 进行任何类型的 sql 日志记录的替代方法.

There is an alternative way of using log4jdbc for any kind of sql logging.

以上答案假设您在 JBoss 上运行使用 hibernate 的代码,而不是在 IDE 中.在这种情况下,您还应该在 serverdefaultdeployjboss-logging.xml 中的 JBoss 上配置日志记录,而不是在本地 IDE 类路径中.

The above answer assumes that you run the code that uses hibernate on JBoss, not in IDE. In this case you should configure logging also on JBoss in serverdefaultdeployjboss-logging.xml, not in local IDE classpath.

请注意,JBoss 6 默认不使用 log4j.因此,将 log4j.properties 添加到 ear 无济于事.尝试添加到 jboss-logging.xml 中:

Note that JBoss 6 doesn't use log4j by default. So adding log4j.properties to ear won't help. Just try to add to jboss-logging.xml:

   <logger category="org.hibernate">
     <level name="DEBUG"/>
   </logger>

然后更改根记录器的阈值.请参阅 SLF4J logger.debug() 未登录JBoss 6.

Then change threshold for root logger. See SLF4J logger.debug() does not get logged in JBoss 6.

如果您设法直接从 IDE 调试休眠查询(无需部署),那么您应该在类路径上有 log4j.properties、log4j、slf4j-api 和 slf4j-log4j12 jar.请参阅 http://www.mkyong.com/hibernate/how-to-configure-log4j-in-hibernate-project/.

If you manage to debug hibernate queries right from IDE (without deployment), then you should have log4j.properties, log4j, slf4j-api and slf4j-log4j12 jars on classpath. See http://www.mkyong.com/hibernate/how-to-configure-log4j-in-hibernate-project/.

这篇关于启用休眠日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆