web-dev-qa-db-fra.com

journalisation des valeurs de paramètre hibernate à l'aide de logback et de slf4j

J'utilise Hibernate, Spring MVC et Eclipse. Dans ma console Eclipse, l'hibernate sql s'affiche sous la forme de: 

Hibernate: insert into some_table (fieldname1, fieldname2, fieldname3, fieldname4)  
values (?, ?, ?, ?)

Comment puis-je obtenir de la console pour imprimer les valeurs qui sont insérées à la place des points d'interrogation? Je m'engage à utiliser slf4j et à me connecter pour me connecter à mon application. 

Voici mon logback.xml: 

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <!-- To enable JMX Management -->
    <jmxConfigurator/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>

    <!--<logger name="org.hibernate" level="debug"/> -->
    <logger name="mypackagename.myappname" level="debug"/>
    <logger name="org.hibernate.SQL" additivity="false" level="DEBUG" />
    <logger name="org.hibernate.type" additivity="false" level="TRACE" />

    <root level="info">
        <appender-ref ref="console"/>
    </root>
</configuration>
11
CodeMed

La réponse correcte s'est avérée être: 

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <!-- To enable JMX Management -->
    <jmxConfigurator/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-5level %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="mypackagename.myappname" level="TRACE"/>
    <logger name="org.hibernate.SQL" level="DEBUG" />
    <logger name="org.hibernate.type" level="TRACE" />

    <root level="info">
        <appender-ref ref="console"/>
    </root>
</configuration>
14
CodeMed

Configurez le package org.hibernate.type.descriptor.sql.BasicBinder pour qu'il enregistre le niveau TRACE:

 <logger name="org.hibernate.type.descriptor.sql.BasicBinder" additivity="false" level="TRACE" />
3
Andrey M

Voici quelques informations sur les 2 réponses proposées. Les deux travaillaient pour moi. 

Pour imprimer la requête souhaitée: 

<logger name="org.hibernate.SQL" additivity="false" >
    <level value="DEBUG" />
    <appender-ref ref="SQLROLLINGFILE" />
    <appender-ref ref="STDOUT" />
</logger>

Pour l’imprimer "joli", vous pouvez utiliser la jpaProperties.put("hibernate.format_sql", true|false); Plus d’infos dans [ https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/configuration-optional. html]

Maintenant en ce qui concerne les valeurs. Les acceptés: 

<logger name="org.hibernate.type" additivity="false" >
    <level value="TRACE" />
    <appender-ref ref="SQLROLLINGFILE" />
    <appender-ref ref="STDOUT" />
</logger>

La sortie serait: 

2017-02-12 14:16:57 DEBUG org.hibernate.SQL - 
    select
        producttyp0_.idProductType as idProduc1_25_1_,
        producttyp0_.deleted as deleted2_25_1_,
        producttyp0_.description as descript3_25_1_,
        producttyp0_.name as name4_25_1_,
        products1_.idProductType as idProduc6_25_3_,
        products1_.idProduct as idProduc1_24_3_,
        products1_.idProduct as idProduc1_24_0_,
        products1_.deleted as deleted2_24_0_,
        products1_.maxQty as maxQty3_24_0_,
        products1_.name as name4_24_0_,
        products1_.price as price5_24_0_,
        products1_.idProductType as idProduc6_24_0_ 
    from
        ProductType producttyp0_ 
    left outer join
        Product products1_ 
            on producttyp0_.idProductType=products1_.idProductType 
    where
        producttyp0_.idProductType=?
2017-02-12 14:16:57 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - [35]
2017-02-12 14:16:57 TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([idProduc1_24_0_] : [INTEGER]) - [null]
2017-02-12 14:16:57 TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([deleted2_25_1_] : [INTEGER]) - [0]
2017-02-12 14:16:57 TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([descript3_25_1_] : [VARCHAR]) - [desc]
2017-02-12 14:16:57 TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([name4_25_1_] : [VARCHAR]) - [c0my6zko[test]]
2017-02-12 14:16:57 TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([idProduc6_25_3_] : [INTEGER]) - [null]

Et l'autre solution proposée 

<logger name="org.hibernate.type.descriptor.sql.BasicBinder" additivity="false" level="TRACE" >
    <level value="TRACE" />
    <appender-ref ref="SQLROLLINGFILE" />
    <appender-ref ref="STDOUT" />
</logger>

La sortie serait: 

2017-02-12 14:18:55 DEBUG org.hibernate.SQL - 
    select
        producttyp0_.idProductType as idProduc1_25_1_,
        producttyp0_.deleted as deleted2_25_1_,
        producttyp0_.description as descript3_25_1_,
        producttyp0_.name as name4_25_1_,
        products1_.idProductType as idProduc6_25_3_,
        products1_.idProduct as idProduc1_24_3_,
        products1_.idProduct as idProduc1_24_0_,
        products1_.deleted as deleted2_24_0_,
        products1_.maxQty as maxQty3_24_0_,
        products1_.name as name4_24_0_,
        products1_.price as price5_24_0_,
        products1_.idProductType as idProduc6_24_0_ 
    from
        ProductType producttyp0_ 
    left outer join
        Product products1_ 
            on producttyp0_.idProductType=products1_.idProductType 
    where
        producttyp0_.idProductType=?
2017-02-12 14:18:55 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - [36]

Personnellement, j’aime bien le deuxième parce qu’il ya moins d’informations (en évitant le o.h.t.descriptor.sql.BasicExtractor), mais c’est à la hauteur du projet. 

J'espère que cela vous donnera un peu plus d'informations sur ce qu'il faut inclure. 

0
cesaregb