5

我正在尝试将 logback 与 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>

控制台输出

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 左外连接 ts_utenti_anagrafica utentianag4_ on this_.id_utente_anagrafica=utentianag4_.id_utente_anagrafica where this_.username=? 和 this_.password=?

4

2 回答 2

1

您在那里看到的控制台输出是因为您在休眠配置中有这个:

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

这告诉 hibernate 将 sql 准备好的语句记录到控制台。

很难从您的问题中判断 logback 日志记录是否正常工作,但如果它不在这里,这是一个很好的(如果有点过时;只需更新版本号)教程来启动和运行它。

编辑

如果上述教程不起作用,那么很可能您使用的是 Hibernate 4 版本。

在更高版本的 hibernate 中,日志框架从 slf4j 切换到 JBoss 日志。这意味着 slf4j 不再是 Hibernate 依赖项。

您需要将最新的 slf4j JAR 添加到类路径中。

于 2013-04-10T08:40:00.417 回答
1

如果您想记录所有 Hibernate 内容(这些日志很多,并且将是一个真正的性能问题),请使用TRACE日志级别而不是ALL.

于 2013-04-10T21:53:25.883 回答