-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Logging SQL with Log4j - problem
PostPosted: Fri Nov 02, 2007 11:54 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
My current log4j properties file has the following input regarding Hibernate:



Code:
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=warn, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=DEBUG
log4j.logger.org.hibernate=debug, stdout
log4j.logger.org.hibernate.SQL=debug, stdout
log4j.logger.org.hibernate.type=info, stdout
log4j.logger.org.hibernate.engine.QueryParameters=debug


and the file is in the lib folder of my web application.

Unfortunately, I cannot get any Hibernate logs on my tomcat logging file unless I explicitly set the attribute

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

on my hibernate.cfg.xml file. (and I can't get my queryparameters, on which I am interested in).

Can anyone help me understand what am I doing wrong for the hibernate not being logged?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 03, 2007 11:51 pm 
Newbie

Joined: Wed Oct 04, 2006 2:47 am
Posts: 12
Try moving the log4j.properties file to WEB-INF/classes/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 12:15 pm 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
tima wrote:
Try moving the log4j.properties file to WEB-INF/classes/


Produces no effect. Removing the
<property name="show_sql">true</property>

proprety turns logging off, even though I have all the log4j.logger.org.hibernate settings in trace level!

Isn't the show_sql property supposed to be optional if you have the log4j property set?

And why can't I log the query parameters, no matter what option I use?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 11:36 pm 
Newbie

Joined: Wed Oct 04, 2006 2:47 am
Posts: 12
The show_sql property just enables logging to System.out.
The logging equivalent is the org.hibernate.SQL category.

Do you have the log4j in your WEB-INF/lib dir?

If you are still having problems, and are using tomcat, try placing a copy of log4j.properties in common/classes or shared/classes.
You can also get diagnostic output from log4j by specifying -Dlog4j.debug=true on the webserver command line.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 5:45 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
tima wrote:
The show_sql property just enables logging to System.out.
The logging equivalent is the org.hibernate.SQL category.

Do you have the log4j in your WEB-INF/lib dir?

If you are still having problems, and are using tomcat, try placing a copy of log4j.properties in common/classes or shared/classes.
You can also get diagnostic output from log4j by specifying -Dlog4j.debug=true on the webserver command line.


I am using tomcat 6.

I have tried placing the log4j.properties file in both the /lib and /classes directory of my web application, without success. Logging of other packages works regardless of the directory the file is in, because when I move it I update its location in my servlet description in web.xml:

Code:
   <servlet>
      <servlet-name>LogServelet</servlet-name>
      <servlet-class>aux.servlet.LogServlet</servlet-class>
      <init-param>
         <param-name>log4jconfig</param-name>
         <param-value>/WEB-INF/classes/log4j.properties</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 12:11 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
So, let me see if I get this straight:

- If the property "show_sql" is set to false or not set, you cannot get SQL logged anywhere (not to the console, not to any logging file) regardless of whatever log4j settings you use.

- If the property "show_sql" is set to true, you have the ability to log it (or not) using log4j configuration, but you can't avoid the SQL to show in your console, regardless of your log4j configuration.


Are the 2 statements above correct? It is what my personal experience shows.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 11:15 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
gonzao_diaz wrote:
So, let me see if I get this straight:

- If the property "show_sql" is set to false or not set, you cannot get SQL logged anywhere (not to the console, not to any logging file) regardless of whatever log4j settings you use.

- If the property "show_sql" is set to true, you have the ability to log it (or not) using log4j configuration, but you can't avoid the SQL to show in your console, regardless of your log4j configuration.


Are the 2 statements above correct? It is what my personal experience shows.


Yes, this is, as far as I remember, what happens.

After several hours trying to make it work, I eventually gave up on it.


Top
 Profile  
 
 Post subject: logging SQL
PostPosted: Sat Jan 05, 2008 4:12 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Actually, I was wrong.
Probably I had some classes/libraries problem that prevented things from working correctly.

What tima said was correct, hibernate.show_sql just forces the SQL output to the console (indeed annoying of you don't want that), but want to retrieve the SQL by some other means.

Follow this checklist and you will be able to control what you see in terms of SQL code.

1) set the hibernate.show_sql to false in your connections
2) in your web.xml, add the following

Code:
    <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>/WEB-INF/classes/log4j.xml</param-value>
    </context-param>


and, if you are using Spring, this other fragment

Code:
<listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

3) add the following file log4j.xml under the WEB-INF/classes directory of your deployed program (forget about the web server's directories)

Code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

   <appender name="appenderConsole" class="org.apache.log4j.ConsoleAppender">

     <layout class="org.apache.log4j.SimpleLayout"/>
   </appender>


   <appender name="appenderFile" class="org.apache.log4j.RollingFileAppender">
     <param name="File" value="/logs/pubh3.javalog"/>
     <param name="Append" value="true"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="1"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
     </layout>
   </appender>
   
   <appender name="appenderFileDBCon" class="org.apache.log4j.RollingFileAppender">
     <param name="File" value="/logs/pubh3.dbcon"/>
     <param name="Append" value="true"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="1"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
     </layout>
   </appender>
   
   
    <logger name="org.hibernate.SQL" additivity="false">
      <level value="debug" />
      <appender-ref ref="appenderFile" />
    </logger>
   
   <root>
        <level value="info"/>
       <appender-ref ref="appenderConsole" />
       <appender-ref ref="appenderFile" />
   </root>

</log4j:configuration>


As you can see, by taking in and out those appenders in <root>, you can decide what shows or not in the console and in the log file. You could also easily create a new file appender, just for the Hibernate SQL.

4) For good measure, these are my libraries (they should be under WEB-INF/lib of your deployed application, independently of what you have on your web server:

commons-logging-1.1.1.jar
log4j-1.2.15.jar

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.