2016-11-30 82 views
0

我需要登錄所有的全查詢(帶參數)在休眠5 我的build.gradle(依賴):Hibernate日誌本地的SQL查詢

dependencies { 
    compile 'com.corundumstudio.socketio:netty-socketio:1.7.8' 
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21' 
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21' 
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7' 
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7' 
    compile 'org.hibernate:hibernate-core:5.2.5.Final' 
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38' 
} 

我log4j2-test.properties:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n 

log4j.rootLogger=info, stdout 
# basic log level for all messages 
log4j.logger.org.hibernate=INFO 

# SQL statements and parameters 
log4j.logger.org.hibernate.SQL=debug 
log4j.logger.org.hibernate.type=ALL 
log4j.logger.org.hibernate.type.descriptor.sql=trace 

但是Hibernate不想按預期工作。

回答

0

我使用XML配置格式,這適用於我。

<Logger name="org.hibernate.SQL" level="debug" additivity="false"> 
    <AppenderRef ref="main_appender"/> 
</Logger> 
<Logger name="org.hibernate.type" level="trace" additivity="false"> 
    <AppenderRef ref="main_appender"/> 
</Logger> 

我它是如何轉換的猜測是有log4j.logger.org.hibernate.type=trace

1

你的依賴是稍有不妥。

請刪除org.slf4j:slf4j-log4j12(這是舊的Log4j 1)並添加org.apache.logging.log4j:slf4j-impl

+0

並在命令行中設置'-Dorg.jboss.logging.provider = slf4j'或調用'System.setProperty(「org.jboss.logging.provider」,「slf4j」);'在Hibernate初始化之前。 – coladict