启用休眠日志记录

2022-09-01 18:32:51

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

### 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子句或插入语句中看到值(实际上我只看到一些'?')提前致谢。

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

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

答案 1

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

添加行

hibernate.show_sql=true
hibernate.format_sql=true

要么

server\default\deployers\ejb3.deployer\META-INF\jpa-deployers-jboss-beans.xml

或应用程序的 in 标记。persistence.xml<persistence-unit><properties>

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

还有一种将 log4jdbc 用于任何类型的 sql 日志记录的替代方法。

上面的答案假定您在 JBoss 上运行使用休眠的代码,而不是在 IDE 中运行。在这种情况下,您还应该在服务器\default\deploy\jboss-logging.xml的 JBoss 上配置日志记录,而不是在本地 IDE 类路径中配置日志记录。

请注意,JBoss 6 默认情况下不使用 log4j。因此,将log4j.properties添加到耳朵中将无济于事。只需尝试添加到 jboss-logging.xml:

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

然后更改根记录器的阈值。请参阅 SLF4J logger.debug() 不会在 JBoss 6 中登录

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


答案 2

我对自己负责。正如Vadzim所建议的那样,我必须考虑jboss-logging.xml文件并插入以下行:

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

我没有编写 DEBUG 级别,而是编写了 TRACE。现在,不要只查看控制台,而是打开服务器.log文件(调试消息不会发送到控制台,但您可以配置此模式!


推荐