17

I have crearte log4j.properties file like below:

log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug

log4j.appender.hb=org.apache.log4j.ConsoleAppender
log4j.appender.hb.layout=org.apache.log4j.PatternLayout
log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
log4j.appender.hb.Threshold=TRACE

Can someone help me to how to include it in hibernate.cfg.xml file? I am sorry I actually don't know how log4j works. I create this to display my hibernate query with value instead of ? but still it displays ? nothing changes so what I need to proceed further?

I took reference from here Hibernate show real SQL

Community
  • 1
  • 1
commit
  • 4,777
  • 15
  • 43
  • 70
  • 1
    You need not include it. Place log4j.properties file in classpath and hibernate will automatically pick that up. Please make sure, you provide the required jar files. – Abhijith Nagarajan Sep 30 '13 at 10:38
  • I had place it in default `src` folder and it is in `classpath` but I did not seen any effect. – commit Sep 30 '13 at 11:02
  • Can you make sure you have the log4j.properties in classpath? For me it works.Below is my project structure src/main/java --> Java classes src/main/resources --> log4j.properties. Though this is maven project structure. I am using normal eclipse build (Project --> build project) to build my classes – Abhijith Nagarajan Sep 30 '13 at 11:37
  • 1
    What version of hibernate do you have? Hibernate 3.5 uses SLF4j api, which uses logback by default instead of log4j. If you have logback in your classpath, remove it and add slf4j-log4j12.jar and log4j.jar instead. – GeertPt Oct 01 '13 at 11:28

8 Answers8

14

The 'show_sql' property in your hibernate.cfg.xml causes the queries to be printed directly to the console.

Log4j allows output to be logged anywhere from console to file to network ports to databases. But the simple configuration that you have is supposed to print on the console as well. So first remove the show_sql property to see if Log4j puts anything on the console at all.

If this doesn't work, it shows that Log4j is not configured correctly. If you're using hibernate > 3.5, it uses the slf4j api, which uses logback by default instead of log4j. You can easily switch to log4j by removing logback jar(s) from your classpath, and adding slf4j-log4j12.jar and log4j.jar instead.

The Log4j tracing also prints the queries using '?', but it also prints the parameter bindings, i.e. what the '?' will be replaced with by the database driver or server.

GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • Your answer seems to be right, logback is configured in my project before and that's why it is not getting log4j, I can't remove it so i need to find solution in logback, thanks for understanding me what the problem was. Thanks.. – commit Oct 06 '13 at 10:45
  • 1
    @commit, if you want to use logback just drop log4j-over-slf4j.jar into classpath and don't bother with log4j configuration at all. – Vadzim Oct 07 '13 at 05:15
  • 1
    Hibernate 4 uses JBoss logging instead of slf4j. See http://docs.jboss.org/hibernate/orm/4.3/topical/html/logging/Logging.html – G. Stevens Mar 01 '17 at 16:03
3

Please refer hibernate reference as per your version. Here is 3.3 Link

Edit :-

  1. In order to setup logging you will need slf4j-api.jar in your classpath together with the jar file for your preferred binding - slf4j-log4j12.jar in the case of Log4J.
  2. Put log4j.properties file in your classpath. An example properties file is distributed with Hibernate in the src/ directory.
  3. Enable following log 4j categories.

    org.hibernate.SQL   Log all SQL DML statements as they are executed
    
    org.hibernate.type  Log all JDBC parameters
    
bsingh
  • 145
  • 1
  • 6
2

In most cases it should be sufficient to include log4j.properties in application's classpath.

See How to initialize log4j properly?.

The link in question already suggests that you should look for bound sql param values not in sql text by in separate log statements like this: TRACE [BasicBinder] binding parameter [1] as [VARCHAR] - john doe.

But I recommend to stick with log4jdbc. It's pretty simple to use. And it's able to inline query param values in printed sql text.

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
2

Add this also

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

your problem will be fixed.

jdev
  • 5,304
  • 1
  • 17
  • 20
  • `use_sql_comments` will just displays sql comments, it does not resolve my problem. – commit Oct 06 '13 at 10:17
  • 2
    For the non-xml properties: log4j.logger.org.hibernate.SQL=DEBUG log4j.logger.org.hibernate.TYPE=DEBUG log4j.logger.org.hibernate.USE_SQL_COMMENTS=DEBUG – Alan B. Dee Jan 17 '14 at 17:22
0

No need to add in hibernate.cfg.xml, just place it to classpath and follow the steps mentioned in this link. refer this link :- http://makecodeeasy.blogspot.in/2013/03/logger-in-spring-web-application.html

Rahul Jain
  • 658
  • 5
  • 20
0

You can't print the values inline. Hibernate will always print the ? when logging the queries. The output of org.hibernate.type logging at the DEBUG level shows the values and types that are used to replace those question marks.

Coffee Monkey
  • 481
  • 2
  • 7
0

i like sql logging with proxy of jdbcdslog much more than hibernate logging.

Log SQL that is communicated with the database

Community
  • 1
  • 1
Frank M.
  • 997
  • 1
  • 10
  • 19
0

In my case it wasn't hibernate.cfg.xml, it was log4j.xml file where I put the code given by @jdev

< property name="show_sql">true Shows SQL queries

< property name="format_sql">true Format the query shown on console

< property name="use_sql_comments">true comments will be added to the queries

If your project is having 1000s of queries printing to the console, make others false: < property name="show_sql">true < property name="format_sql">false < property name="use_sql_comments">false

OR write an appender to the log4j.xml which I have done in my project, just to write all SQL queries on separate text file which is specified here: Configuring Hibernate logging using Log4j XML config file?

Community
  • 1
  • 1