Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include log4j properties file in hibernate to show query with value instead of question mark

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

like image 821
commit Avatar asked Sep 05 '13 11:09

commit


People also ask

Where should I put log4j properties file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

Does hibernate use log4j?

Hibernate uses Simple Logging Facade for Java (SLF4J) to redirect the logging output to your perfer logging frameworkis (log4j, JCL, JDK logging, lofback…).


2 Answers

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.

like image 90
GeertPt Avatar answered Sep 25 '22 00:09

GeertPt


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
    
like image 3
bsingh Avatar answered Sep 26 '22 00:09

bsingh