Logback与Hibernate不兼容,无法正常工作。

5
我正在尝试使用logback和hibernate,看起来很简单,但我无法使其正常工作。
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:选择 this_.id_utente 作为 id1_2_3_,this_.valido 作为 valido2_3_,aziendagru2_.id_azienda_gruppo 作为 id1_0_0_,aziendagru2_.logo 作为 logo0_0_,aziendagru2_.nome_azienda 作为 nome3_0_0_,aziendagru2_.sottotitolo_due 作为 sottotit4_0_0_,aziendagru2_.sottotitolo_uno 作为 sottotit5_0_0_,livello3_.id_livello 作为 id1_3_1_,livello3_.descrizione 作为 descrizi2_3_1_,utentianag4_.id_utente_anagrafica 作为 id1_1_2_,utentianag4_.cognome 作为 cognome1_2_,utentianag4_.nome 作为 nome1_2_,来自 ts_utenti 的 this_ 左外连接 ts_aziende_gruppo aziendagru2_,条件为 this_.id_azienda_gruppo=aziendagru2_.id_azienda_gruppo 左外连接 ts_livelli livello3_,条件为 this_.id_livello=livello3_.id_livello 左外连接 ts_utenti_anagrafica utentianag4_,条件为 this_.id_utente_anagrafica=utentianag4_.id_utente_anagrafica,where this_.username=? and this_.password=?


我觉得没问题,你具体有什么问题? - Boris the Spider
1
我应该看到类似于“2011-04-23_14:34:08.056 [main] DEBUG select this_.id_utente as id1_2_3”的输出。如果我不使用logback,那么这个“hibernate:select this....”的输出是相同的。 - Federik
请使用 DEBUG 代替 ALL - Amir Pashazadeh
2个回答

1
您在控制台看到的输出是因为在您的Hibernate配置文件中有以下内容:
<property name="hibernate.show_sql">true</property>

这告诉Hibernate将SQL准备语句记录到控制台中。
很难从你的问题中判断logback日志记录是否正常工作,但如果没有,请参考这里,这是一个非常好的教程(稍微有点过时,请更新版本号)。
编辑:
如果上述教程不起作用,则可能使用的是Hibernate 4的版本。
在Hibernate的较新版本中,日志框架已从slf4j切换到了JBoss logging。这意味着slf4j不再是Hibernate的依赖项。
您需要将最新的slf4j JAR添加到类路径中。

你的问题中没有提到,但是你是否使用了 Hibernate 4? - Boris the Spider
我在我的项目库中没有使用Hibernate 4。以下是我使用的库:Hibernate: 3.3.0ga - Hibernate-commons-annotations: 3.3.0ga - hibernate-core: 3.3.0ga - hibernate-c3p0: 3.3.0ga - sl4j - api: 1.7.2 - Federik
logback-access 1.0.11; logback-classic 1.0.10; logback-core 1.0.10 - Federik
有什么建议吗?但我认为问题是在log4j上,因为我甚至尝试了单独的log4j(而不是logback),它也不起作用。 - Federik
现在很奇怪。当您尝试使用log4j时,是否同样将log4j桥接器添加到类路径中了? - Boris the Spider

1
如果你想记录所有 Hibernate 相关的信息(这些日志非常多,会对性能造成真正的影响),请使用 TRACE 日志级别而不是 ALL

仍然会在控制台输出<property name="hibernate.show_sql">true</property>,而不是使用logback。 - Federik
您尚未为Hibernate记录器分配任何附加器。 - Amir Pashazadeh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接