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