2015-04-17 95 views
1

我是hibernate的新手,我在Eclipse中做項目。當我登錄到我的應用程序會顯示在控制檯的許多日誌消息,我不需要,日誌消息是這樣的,如何在Eclipse中禁用Hibernate日誌?

97 [http-bio-8080-exec-9] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final 
102 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Environment - Hibernate 3.6.10.Final 
103 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Environment - hibernate.properties not found 
105 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 
108 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 
165 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 
165 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml 
220 [http-bio-8080-exec-9] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null 
262 [http-bio-8080-exec-9] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.aurodisplay.its.beans.UserRegisterBean 
296 [http-bio-8080-exec-9] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity com.aurodisplay.its.beans.UserRegisterBean on table users 
334 [http-bio-8080-exec-9] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.aurodisplay.its.beans.AdminInfoBean 
334 [http-bio-8080-exec-9] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity com.aurodisplay.its.beans.AdminInfoBean on table admin_info 
337 [http-bio-8080-exec-9] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.aurodisplay.its.beans.LatitudeBean 
337 [http-bio-8080-exec-9] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity com.aurodisplay.its.beans.LatitudeBean on table latlng 

我的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<session-factory> 

    <!-- Database connection settings --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/its_serverdb</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">root</property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">10</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 

    <property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property> 
    <property name="hibernate.connection.release_mode">on_close</property> 
    // next are mappings 

我已經集成Log4j的,這是我的log4j.xml文件

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> 

<appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <layout class="org.apache.log4j.PatternLayout"> 
    <param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p :%L - %m%n" /> 
    </layout> 
</appender> 

<appender name="file" class="org.apache.log4j.RollingFileAppender"> 
    <param name="append" value="false" /> 
    <param name="maxFileSize" value="10MB" /> 
    <param name="maxBackupIndex" value="10" /> 
    <param name="file" value="${catalina.home}/logs/ITS_Server.log" /> 
    <layout class="org.apache.log4j.PatternLayout"> 
    <param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%c{1}] %-5p:%L - %m%n" /> 
    </layout> 
</appender> 

<category name="org.hibernate"> 
    <priority value="DEBUG" /> 
</category> 

<category name="java.sql"> 
    <priority value="debug" /> 
</category> 

<logger name="org.hibernate"> 
    <level value="info"/> 
</logger> 

<root> 
    <level value="DEBUG" /> 
    <appender-ref ref="console" /> 
    <appender-ref ref="file" /> 


    </root> 

</log4j:configuration> 

我該如何禁用它。當我谷歌關於它,somany解決方案正在使用Log4j我不知道它是什麼,它是什麼。如果可能的話,我可以在不使用Log4j的情況下禁用日誌嗎?請告訴我如何。謝謝。

回答

0

當你沒有任何記錄器,嘗試在hibernate.cfg.xml文件中添加此

<!-- Limit the org.hibernate category to INFO since logging to DEBUG affects performance badly --> 
<category name="org.hibernate"> 
    <priority value="WARN"/> 
</category> 

O寫你的日誌信息到一個文件,當你安裝Log4j時,你必須使用org.apach e.log4j.FileAppender

下面是FileAppender一個示例配置文件log4j.properties -

# Define the root logger with appender file 
log4j.rootLogger = DEBUG, FILE 

# Define the file appender 
log4j.appender.FILE=org.apache.log4j.FileAppender 

# Set the name of the file 
log4j.appender.FILE.File=${log}/log.out 

# Set the immediate flush to true (default) 
log4j.appender.FILE.ImmediateFlush=true 

# Set the threshold to debug mode 
log4j.appender.FILE.Threshold=debug 

# Set the append to false, overwrite 
log4j.appender.FILE.Append=false 

# Define the layout for file appender 
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE.layout.conversionPattern=%m%n 

檢查herehere進一步的信息。

+0

我不使用'Log4j'(我沒有涉及到'Log4j'任何外部罐)配置log4j。然後它也會工作。 – Raghu

+0

請看我編輯的問題 – Raghu

+0

請幫忙。我需要解決這個問題 – Raghu

0

設置

<property name="show_sql">false</property> 

當創建會話使用

Logger log = Logger.getLogger("org.hibernate"); 
    log.setLevel(Level.your_level); 

這應該停止記錄,你不必使用任何xmlcfg文件

+0

我得到錯誤'錯誤無法解決或不是字段' – Raghu

+0

對不起@Raghu我忘記了確切的級別是致命的 – Saif

+0

創建會話時意味着'SessionFactory sFactory = HibernateUtil.getSessionFactory();'在這行之前? – Raghu

0

嘗試在log4j的follwing線.XML。

<Root> 
<Level value = "INFO" /> 
<Appender-ref ref = "console" /> 
<Appender-ref ref = "rootLogger" /> 
</Root> 

我在log4j.properties中使用了以下行,這對我來說很合適。

log4j.rootLogger=DEBUG, file, console 
log4j.appender.file=org.apache.log4j.RollingFileAppender 
log4j.appender.file.File=../logs/Log_report.log 
log4j.appender.file.MaxFileSize=1MB 
log4j.appender.file.MaxBackupIndex=1 
log4j.appender.file.layout=org.apache.log4j.PatternLayout 
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n 
+0

可以請您上網聊天。我想討論這一點。 – Raghu

+0

我不需要爲休眠日誌定義任何類別 – Raghu

+0

請查看我更新的問題,我添加了我的Log4j.xml文件。請告訴我我必須做出哪些改變。 – Raghu

0

舊的職位,但有用的:

首先,你可以使用log4j的配置文件(添加到您的pom.xml)

<dependency> 
    <groupId>log4j</groupId> 
    <artifactId>log4j</artifactId> 
    <version>1.2.17</version> 
</dependency> 

最後,禁用日誌,放在SRC /主/資源/ log4j的。屬性文件

log4j.rootLogger=OFF