0

I'm a newbie when it comes to spring so this maybe something simply I'm over looking but I've been trying for hours to no avail. I have spring and hibernate integrated and working however, I want to log the queries to a file so that I can examine them. I'm using log4j for the logging but the issue is I can't get hibernate to write to the file and was hoping to get some clarification.

Here is the sessionFactory bean in applicationContext

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.example"/>
</bean>

Log4j configuration

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

<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${catalina.home}/logs/my-log-file.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="20MB" />
    <param name="MaxBackupIndex" value="2" />

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE} %c | %msg %n" />
    </layout>
</appender>

<appender name="hibernate-rolling" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${catalina.home}/logs/hib-example-file.log" />
    <param name="Append" value="true" />
    <param name="ImmediateFlush" value="true" />
    <param name="MaxFileSize" value="20MB" />
    <param name="MaxBackupIndex" value="2" />

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE} %c | %msg %n"/> 
    </layout>
</appender>


<logger name="org.hibernate.SQL" additivity="false">
    <level value="debug" />
    <appender-ref ref="hibernate-rolling" /> 
</logger>


<root>
    <priority value="INFO"></priority>
    <appender-ref ref="FILE" />
</root>
</log4j:configuration>

Any help is much appreciated?

EDIT:

I have tried the loggers that were provided in that link but that did not cause the queries to be written to the file.

Community
  • 1
  • 1
Chris
  • 21
  • 1
  • 5
  • Change your level to ALL instead of debug. – Mark Leiber May 18 '15 at 18:52
  • I have tried adding all of those loggers as well and changed the level and still no change. If I change the logger to org.hibernate and the level to debug it writes to the log file but it's 2000+ lines and not one of them is the query that is used to retrieve the object I need. – Chris May 18 '15 at 19:01
  • org.hibernate.SQL works for me to show all queries when I have the level set to ALL. Stupid question, but are you looking at the right file for your output (hib-example-file.log), and did you restart your application after changing your config? – Mark Leiber May 18 '15 at 19:23
  • Yes, I am. What is strange is that when I set up log4j and hibernate in a Java Swing application I had no issue. I basically copied over the log4j.xml to the spring project and change the file locations and I don't get nothing to print to the log file. – Chris May 18 '15 at 19:25
  • I'm a complete idiot and just released my problem and it had nothing to do with the logger. I forgot to add the jpa annotation for the table to the model so the query was never called. What's strange is that no exceptions were thrown. I assumed something like that would cause an exception. Thank for you help and sorry for wasting your time. – Chris May 18 '15 at 19:36

1 Answers1

0

I was missing the Table JPA annotation causing the query to never actually execute. Therefor, it didn't log it. Added @Table(name = "My_Table") and it worked.

Chris
  • 21
  • 1
  • 5