How to disable Hibernate logs in Eclipse?

2019-08-12 00:40发布

问题:

I am new to hibernate , I am doing project in Eclipse. When I log in to my application it displays to many log messages in console which I dont need, the log message is something like this,

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

My 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

I have integrated Log4j , Here is my Log4j.xml file

<?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>

How can I disable it. When I google about it, somany solutions are using Log4j I dont know what is it and what is it for. can I do disable logs without Log4j if possible please tell me how .It would be greate help. Thank you.

回答1:

set

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

When creating session use

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

This should stop the logging and you don't have to use any xml or cfg file



回答2:

Try follwing lines in log4j.xml.

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

I used following line in log4j.properties which worked for me.

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


回答3:

Try to set

log4j.logger.org.hibernate=info

More info Turning off hibernate logging console output



回答4:

As you dont have any logger, try adding this in your hibernate.cfg.xml file

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

o write your logging information into a file, when you have Log4j installed you would have to use org.apache.log4j.FileAppender.

Following is a sample configuration file log4j.properties for FileAppender −

# 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

Check here and here for further info.



回答5:

Old post, but usefull :

First, you can use log4j configuration file (add this to your pom.xml)

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

Finally, to disable logging, put in src/main/resources/log4j.properties file

log4j.rootLogger=OFF