5

I am using log4j2 with slf4j and hibernate 4.3.1. I am having trouble showing the parameters of the SQL queries. I did not find anywayt to show those (SQL queries are well shown but without parameters). Here is what I have specified in my persistence.xml:

<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="xxxxx" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.archive.autodetection" value="class" />
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.use_sql_comments" value="true"/>
        <property name="hibernate.query.factory_class"
            value="org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory" />
        <property name="max_fetch_depth" value="2" />
        <property name="hibernate.jdbc.batch_size" value="100" />
    </properties>
</persistence-unit>

I have defined this in my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" verbose="true">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%m%n" />
        </Console>
        <File name="MyFile" fileName="test.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="MyFile" />
        </logger>
        <logger name="org.hibernate.type" level="TRACE" additivity="false">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="MyFile" />
        </logger>
        <Root level="TRACE">
            <AppenderRef ref="STDOUT" />
            <AppenderRef ref="MyFile" />
        </Root>
    </Loggers>
</Configuration>
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Abbadon
  • 2,513
  • 3
  • 25
  • 33
  • Duplicate of: http://stackoverflow.com/questions/1710476/print-query-string-in-hibernate-with-parameter-values – John Ament Feb 13 '14 at 12:33
  • 2
    It's not. I have tested the solutions proposed and it was not working. I think it is linked to the version of hibernate. Here is what i found in the hibernate documentation: "Logging Important Completely out of date. Hibernate uses JBoss Logging starting in 4.0. This will get documented as we migrate this content to the Developer Guide." – Abbadon Feb 13 '14 at 12:38
  • Please update your question with your logging configuration based on the tips in that SO question. JBoss Logging is simply a facade for other logging frameworks, it's internally the jboss logging framework similar to what SLF4J provides. – John Ament Feb 13 '14 at 12:42
  • true not working ? – Exorcismus Feb 13 '14 at 12:43

2 Answers2

2

First, make sure you have the log4j-core-2.0, log4j-api-2.0 and log4j-1.2-api-2.0 jars in the classpath.

Next, JBoss support for Log4J2 is fairly recent. You may need JBoss Logging 3.1.4 to be able to use Log4J2 (see JBLOGGING-94). From JBoss Logging 3.2 there will be performance improvements (see JBLOGGING-95) and you may be able to omit the log4j-1.2-api-2.0 bridge jar.

Please note that the current JBoss-Logging Beta1 (3.2.0.Beta1) has a NPE when logging: JBLOGGING-107. So having it may be necessary to tell hibernate to use the slf4j API using the org.jboss.logging.provider System Property (see org.jboss.logging.LoggerProviders).

wemu
  • 7,952
  • 4
  • 30
  • 59
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0
<!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
Ganesh Giri
  • 1,135
  • 11
  • 18