[java] Enable Hibernate logging

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

This question is related to java hibernate logging log4j jboss6.x

The answer is


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


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

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


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.


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to hibernate

Hibernate Error executing DDL via JDBC Statement How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory Disable all Database related auto configuration in Spring Boot Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] HikariCP - connection is not available Hibernate-sequence doesn't exist How to find distinct rows with field in list using JPA and Spring? Spring Data JPA and Exists query

Examples related to logging

How to redirect docker container logs to a single file? Console logging for react? Hide strange unwanted Xcode logs Where are logs located? Retrieve last 100 lines logs Spring Boot - How to log all requests and responses with exceptions in single place? How do I get logs from all pods of a Kubernetes replication controller? Where is the Docker daemon log? How to log SQL statements in Spring Boot? How to do logging in React Native?

Examples related to log4j

No log4j2 configuration file found. Using default configuration: logging only errors to the console How to stop INFO messages displaying on spark console? What is log4j's default log file dumping path How to give environmental variable path for file appender in configuration file in log4j No appenders could be found for logger(log4j)? SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". in a Maven Project log4j:WARN No appenders could be found for logger (running jar file, not web app) Error: "setFile(null,false) call failed" when using log4j java.lang.ClassNotFoundException: org.apache.log4j.Level How can I create 2 separate log files with one log4j config file?

Examples related to jboss6.x

Enable Hibernate logging