views:

17295

answers:

8

I haven't been able to find any documentation on how to configure Hibernate's logging using the XML style configuration file for Log4j.

Is this even possible or do I have use a properties style configuration file to control Hibernate's logging?

If anyone has any information or links to documentation it would appreciated.

EDIT:
Just to clarify, I am looking for example of the actual XML syntax to control Hibernate.

EDIT2:
Here is what I have in my XML config file.

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="info"/>
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
        </layout>
    </appender>
    <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="Program-Name.log"/>
        <param name="MaxFileSize" value="1000KB"/>
    <!-- Keep one backup file -->
        <param name="MaxBackupIndex" value="4"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
        </layout>
    </appender>

    <root>
        <priority value ="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="rolling-file" />
    </root>
</log4j:configuration>

Logging works fine but I am looking for a way to step down and control the hibernate logging in way that separate from my application level logging, as it currently is flooding my logs. I have found examples of using the preference file to do this, I was just wondering how I can do this in a XML file.

A: 

Hibernate uses Apache Commons logging, which means you can use log4j as your implementation.

Of course you can use log4j.xml - just put it in your CLASSPATH.

This link explains all.

duffymo
Yeah it on the Classpath, it's a valid configuration. You say I can, but how do I? What is the actual XML?
James McMahon
Actually they are on SLF4J now... http://www.hibernate.org/hib_docs/v3/reference/en-US/html_single/#configuration-logging
Loki
A: 

Here's the simplest log4j.xml I can think of. Amend as you see fit:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
                 xmlns:log4j='http://jakarta.apache.org/log4j/'&gt;

   <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
      </layout>
   </appender>

   <root>
       <level value="debug"/>
       <appender-ref ref="consoleAppender"/>
   </root>

</log4j:configuration>
duffymo
If deployed in an application that did a bare minimum of work, this would fill up the catalina.out (if using Tomcat) pretty quickly. You could change the root logger to, well, nothing and have the Hibernate specific package log using your appender layout. Also, check the 'debug' option in Hibernate.
Joe Liversedge
Ha, I actually tried to post my config, which looks very similar to this, in the question. How did you get XML to post correctly in the code block?I obviously need to clarify my question alittle further. I am looking for away to control hibernate through this file.
James McMahon
I had that problem with XML posting at first. Use the "code" button up above and make sure everything is indented in four spaces - does the trick for me.
duffymo
A: 

Try adding the Hibernate Log4J Appender to the log4j.xml that I posted.

http://www.hibernate.org/97.html

Maybe that's the ticket.

duffymo
Nope, that isn't it. Thanks for trying to help.
James McMahon
+12  A: 

From http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-logging

Here's the list of logger categories:

Category                    Function

org.hibernate.SQL           Log all SQL DML statements as they are executed
org.hibernate.type          Log all JDBC parameters
org.hibernate.tool.hbm2ddl  Log all SQL DDL statements as they are executed
org.hibernate.pretty        Log the state of all entities (max 20 entities) associated with the session at flush time
org.hibernate.cache         Log all second-level cache activity
org.hibernate.transaction   Log transaction related activity
org.hibernate.jdbc          Log all JDBC resource acquisition
org.hibernate.hql.ast.AST   Log HQL and SQL ASTs during query parsing
org.hibernate.secure        Log all JAAS authorization requests
org.hibernate               Log everything (a lot of information, but very useful for troubleshooting) 

NB: Most of the loggers use the DEBUG level, however org.hibernate.type uses TRACE. In previous versions of Hibernate org.hibernate.type also used DEBUG, but as of Hibernate 3 you must set the level to TRACE (or ALL) in order to see the JDBC parameter binding logging.

And a category is specified as such:

<logger name="org.hibernate">
    <level value="ALL" />
    <appender-ref ref="FILE"/>
</logger>

It must be placed before the root element.

Loki
This looks good. Thank you.
James McMahon
I am not sure what <appender-ref> is doing there, when I change it to an appender in my config, hibernate still seems to log to both console and my file appender. Strange.
James McMahon
broken link ` `
Juan Manuel
+2  A: 

In response to homaxto's comment, this is what I have right now.

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="debug"/>
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
        </layout>
    </appender>
    <appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="Program-Name.log"/>
        <param name="MaxFileSize" value="500KB"/>
        <param name="MaxBackupIndex" value="4"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
        </layout>
    </appender>

    <logger name="org.hibernate">
        <level value="info" />
    </logger>

    <root>
        <priority value ="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="rolling-file" />
    </root>
</log4j:configuration>

The key part being

<logger name="org.hibernate">
    <level value="info" />
</logger>

Hope this helps.

James McMahon
A: 

Here's what I use:

<logger name="org.hibernate">
    <level value="warn"/>
</logger>

<logger name="org.hibernate.SQL">
    <level value="warn"/>
</logger>

<logger name="org.hibernate.type">
    <level value="warn"/>
</logger>

<root>
    <priority value="info"/>
    <appender-ref ref="C1"/>
</root>

Obviously, I don't like to see Hibernate messages ;) -- set the level to "debug" to get the output.

TMN
+4  A: 

Loki's answer points to the Hibernate 3 docs and provides good information, but I was still not getting the results I expected.

Much thrashing, waving of arms and general dead mouse runs finally landed me my cheese.

Because Hibernate 3 is using Simple Logging Facade for Java (SLF4J) (per the docs), if you are relying on Log4j 1.2 you will also need the slf4j-log4j12-1.5.10.jar if you are wanting to fully configure Hibernate logging with a log4j configuration file. Hope this helps the next guy.

Dennis S
Yup you need the slf4j-log4j12-1.5.10.jar to wire the facade into the underlying logging layer. The configuration file is still a log4j configuration if you are using log4j as the logging layer.
James McMahon
A: 

How to ON/OFF log files in log4j

Ankita
@Ankita, Sorry but I don't follow your question. Furthermore, answering the question with another question is not a good way to get answers. You want to ask this question (re-phrased) as it's own separate question. It is a much better way to get answers as your question will be a lot more visible on the site.
James McMahon