25

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
Vadzim
  • 24,954
  • 11
  • 143
  • 151
Rosanero4Ever
  • 482
  • 1
  • 7
  • 19

5 Answers5

40

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

Vadzim
  • 24,954
  • 11
  • 143
  • 151
  • I have hibernate.show_sql=true in my persistence.xml but it doesn't work. I'll add hibernate.format_sql=true – Rosanero4Ever Dec 13 '11 at 16:03
  • I have these jars in my classpath and i run my code from netbeans. I added hibernate.format_sql = true but still it doesn't work :-( I'll read something about jboss-logging.xml – Rosanero4Ever Dec 13 '11 at 16:35
  • I doubt you run code in netbeans. IDE just helps you to build ear to deploy on JBoss. Applications code runs actually on appserver that knows nothing about IDE, it's configs or project file structure. JBoss acts depending on just contents of ear and it's own settings. Note that JBoss 6 doesn't use log4j by default. So adding log4j.properties to ear won't help. Keep looking at jboss-logging.xml. – Vadzim Dec 13 '11 at 17:02
  • the code doesn't run in netbeans. I use netbeans for writing my code. Then i deploy the ear on jboss. I hope my explanation is clear. Thanks for your help. I'm reading some docs about jboss.logging.xml but actually i haven't solved my problem – Rosanero4Ever Dec 13 '11 at 17:12
  • So you can freely delete log4j.properties and log4j-1.2.15.jar anywhere in your project. In case your own code doesn't use slf4j both slf4j-api.jar and slf4j-jboss-logmanager.jar can also be deleted. Anyway these jars shouldn't be bundled in ear cause they are already present in jboss-6.0.0.Final\common\lib. – Vadzim Dec 13 '11 at 17:15
  • i've updated the answer with hint on changing jboss-logging.xml. – Vadzim Dec 13 '11 at 17:22
  • I saw these tags but i believe they are useful to limit the verbosity of the log. in this case i should see "only" DEBUG messages but i still see nothing :( – Rosanero4Ever Dec 13 '11 at 17:28
  • Not "only", all higher levels too. See http://stackoverflow.com/questions/3419394/slf4j-logger-debug-does-not-get-logged-in-jboss-6 – Vadzim Dec 13 '11 at 17:35
8

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!).

Rosanero4Ever
  • 482
  • 1
  • 7
  • 19
3

Spring Boot, v2.3.0.RELEASE

Recommended (In application.properties):

logging.level.org.hibernate.SQL=DEBUG //logs all SQL DML statements
logging.level.org.hibernate.type=TRACE //logs all JDBC parameters 

parameters

Note:
The above will not give you a pretty-print though.
You can add it as a configuration:

properties.put("hibernate.format_sql", "true");

or as per below.

Works but NOT recommended

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

Reason: It's better to let the logging framework manage/optimize the output for you + it doesn't give you the prepared statement parameters.

Cheers

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
1

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
Nulldevice
  • 3,926
  • 3
  • 31
  • 37
  • Please verify that any log4j.properties is in your class path. This should not be null (test in Servlet or EJB bean) this.getClass().getClassLoader().getResourceAsStream("log4j.properties") – Nulldevice Dec 13 '11 at 16:55
  • log4j is in my class path. I wrote `InputStream resourceAsStream = null; resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("log4j.properties"); if ( resourceAsStream!=null) out.println("log4j is ok");` and the result is "log4j is ok" – Rosanero4Ever Dec 13 '11 at 17:10
0

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.

user674669
  • 10,681
  • 15
  • 72
  • 105