Sl4j doesn't log sql statements from JPA

1k views Asked by At

I'm developing an app with JPA, Hibernate and Sl4j. I can'm make my sl4j to log sql statements.

Here is my code:

persistence.xml

<persistence-unit name="timeoffcore.persistence.unit">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="show_sql" value="true"/>
    </properties>
</persistence-unit>

log4j.properties

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=timeoff.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
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{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=INFO, file, stdout

But there are no sql statements in log. Neither in console, nor in file.

2

There are 2 answers

1
IMParasharG On BEST ANSWER

property name should be:

  <property name="hibernate.show_sql" value="true" />

in persistence.xml

Add below entry in log4.properties

# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO

# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL
0
Echoinacup On

Probably you should add several lines to log4j.properties

log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE

And two more lines into persistence.xml as well

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

I hope it helps