Enable Hibernate logging

2019-01-11 03:31发布

问题:

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

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

回答1:

Hibernate logging has to be also enabled in hibernate configuration.

Add lines

hibernate.show_sql=true
hibernate.format_sql=true

either to

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

or to application's persistence.xml in <persistence-unit><properties> tag.

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

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

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 server\default\deploy\jboss-logging.xml, not in local IDE classpath.

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>

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

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/.



回答2:

I answer to myself. As suggested by Vadzim, I must consider the jboss-logging.xml file and insert these lines:

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

Instead of DEBUG level I wrote TRACE. Now don't look only the console but open the server.log file (debug messages aren't sent to the console but you can configure this mode!).



回答3:

Your log4j.properties file should be on the root level of your capitolo2.ear (not in META-INF), that is, here:

MyProject
¦   build.xml
¦   
+---build
¦   ¦   capitolo2-ejb.jar
¦   ¦   capitolo2-war.war
¦   ¦   JBoss4.dpf
¦   ¦   log4j.properties


回答4:

We have a tomcat-8.5 + restlet-2.3.4 + hibernate-4.2.0 + log4j-1.2.14 java 8 app running on AlpineLinux in docker.

On adding these 2 lines to /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j.properties, I started seeing the HQL queries in the logs:

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

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

However, the JDBC bind parameters are not being logged.