feder
feder

Reputation: 2058

How to disable hibernate 4 output with log4j2?

I'm having the follwoing log4j2.xml configuration. I saved it in the the root of the resource folder.

I don't get to manage to get rid of the INFO stdout hibernate output. e.g.

06:17:01,835 INFO  [stdout] (ServerService Thread Pool -- 52)     insert into hibernate_sequence values ( 1 )

What is wrong with my configuration and/or what is missing?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.mycode" level="trace" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Logger name="org.hibernate" level="warn" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="warn">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

I have these libraries attached to my project.

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.0.2</version>
        </dependency>

And I'm using this API version of JPA/Hibernate 4.

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <scope>provided</scope>
        </dependency>

Upvotes: 7

Views: 9162

Answers (4)

user5063309
user5063309

Reputation: 11

In your HibernateUtil class, only write:

java.util.logging.Logger.getLogger("org.hibernate")
    .setLevel(java.util.logging.Level.OFF)

before creating an AnnotationConfiguration object.

It works when using log4j2.xml and log4j2 (log4j-api-2.2.jar and log4j-core-2.2.jar)

Here's the code for my function:

private final SessionFactory sessionFactory;
{
    java.util.logging.Logger.getLogger("org.hibernate")
        .setLevel(java.util.logging.Level.OFF);
    try
    {
        sessionFactory = new AnnotationConfiguration()
            .configure("hibernate.cfg.xml").buildSessionFactory();
    } catch (Throwable ex)
    {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
 }

Upvotes: 1

Lokesh Guru
Lokesh Guru

Reputation: 143

I was facing the same issue and turning Log level Off is working for me:

 <logger name="org.hibernate" level="OFF" additivity="false">
        ...
</logger>

Upvotes: 9

Ankur Singhal
Ankur Singhal

Reputation: 26077

Executing:

java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.OFF);

before hibernate's initialization worked for me.

Note: the line above will turn every logging off (Level.OFF). If you want to be less strict, you can use

java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.SEVERE);

that is silent enough. (Or check the java.util.logging.Level class for more levels).

Also the property

hibernate.show_sql
hibernate.generate_statistics
hibernate.use_sql_comments

controls the logging directly to STDOUT bypassing any logging framework (which you can recognize by the missing output formatting of the messages). If you use a logging framework like log4j, you should always set that property to false.

But if you want to disable all console info you must to set the logger level a NONE of FATAL of class org.hibernate..

show_sql

Enable the logging of all the generated SQL statements to the console

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

format_sql

Format the generated SQL statement to make it more readable, but takes up more screen space.

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

use_sql_comments

Hibernate will put comments inside all generated SQL statements to hint what’s the generated SQL trying to do

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

Consolidating:

<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>

Upvotes: 0

Learner
Learner

Reputation: 21445

As per your post, the INFO message contains the queries generated by Hibernate. This is configured in hibernate.cfg.xml configuration file with the property show_sql`. You can disable it using below line in your configuration file.

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

Upvotes: 3

Related Questions