Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback not working with hibernate

i'm trying to use logback with hibernate, seems so easy but i can't get it working

hibernate.cfg.xml

    <!-- DB Properties -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <!-- DB Connection -->
     <property name="hibernate.connection.url"> XXXXXXXXXXXXX</property>
    <property name="connection.username">XXXXXXXXXX</property>
    <property name="connection.password">XXXXXXXXXX</property>


    <!-- c3p0 config  -->

    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>     
    <property name="hibernate.c3p0.acquire_increment">1</property>
    <property name="hibernate.c3p0.idle_test_period">100</property>
    <property name="hibernate.c3p0.min_size">10</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.timeout">1800</property>


</session-factory>

logback.xml

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
        </Pattern>
    </encoder>
</appender>

<appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>c:/timesheet.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
            %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <FileNamePattern>c:/timesheet.%i.log.zip</FileNamePattern>
        <MinIndex>1</MinIndex>
        <MaxIndex>10</MaxIndex>
    </rollingPolicy>

    <triggeringPolicy
        class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <MaxFileSize>2MB</MaxFileSize>
    </triggeringPolicy>

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

console output

Hibernate: select this_.id_utente as id1_2_3_, this_.valido as valido2_3_, aziendagru2_.id_azienda_gruppo as id1_0_0_, aziendagru2_.logo as logo0_0_, aziendagru2_.nome_azienda as nome3_0_0_, aziendagru2_.sottotitolo_due as sottotit4_0_0_, aziendagru2_.sottotitolo_uno as sottotit5_0_0_, livello3_.id_livello as id1_3_1_, livello3_.descrizione as descrizi2_3_1_, utentianag4_.id_utente_anagrafica as id1_1_2_, utentianag4_.cognome as cognome1_2_, utentianag4_.nome as nome1_2_ from ts_utenti this_ left outer join ts_aziende_gruppo aziendagru2_ on this_.id_azienda_gruppo=aziendagru2_.id_azienda_gruppo left outer join ts_livelli livello3_ on this_.id_livello=livello3_.id_livello left outer join ts_utenti_anagrafica utentianag4_ on this_.id_utente_anagrafica=utentianag4_.id_utente_anagrafica where this_.username=? and this_.password=?

like image 316
Federik Avatar asked Nov 04 '22 00:11

Federik


1 Answers

The console output you see there is because you have this in your hibernate config:

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

This tells hibernate to log sql prepared statements to the console.

It's hard to tell from your question whether logback logging is working fine otherwise, but if it isn't here is a pretty good (if a little outdated; just update the version numbers) tutorial on getting it up and running.

EDIT

If the above tutorial is not working then it is likely that you are using a version of Hibernate 4.

In later versions of hibernate the logging framework was switched to JBoss logging from slf4j. This means that slf4j is no longer a Hibernate dependency.

You need to add the newest slf4j JAR to the classpath.

like image 89
Boris the Spider Avatar answered Nov 15 '22 08:11

Boris the Spider