-2

I am working on Hibernate project, But I have to write the logs into a file then both will combined(Hibernate log added in file) How can we disable the hibernate logs.

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.1</version> 
</dependency>
  • Wouldn't setting the log level of org.hibernate to NONE solve this? – Nicktar Jan 14 '20 at 07:31
  • 2
    .. or better to ERROR – Jens Jan 14 '20 at 07:32
  • No, I've used this but its not working for me java.util.logging.Logger.getLogger("org.hibernate").setLevel(java.util.logging.Level.OFF); – Aashish Gossel Jan 14 '20 at 07:35
  • Possible duplicate of https://stackoverflow.com/questions/311408/turning-off-hibernate-logging-console-output – Varun Jain Jan 14 '20 at 08:38
  • I've seen already, Currently In my system (Project logs+Hibernate logs) are printed in same file whenever I turn off the logs then both logs are disabled but I have to do that project logs should be printed and only Hibernate logs turn off. – Aashish Gossel Jan 14 '20 at 09:51

2 Answers2

0

Here are two approaches:

  1. Setting the logging level to NONE should turn off all logging.

  2. Since Hibernate uses the slf4j facade, you should be able to use the NOPLogger; see Get a dummy slf4j logger?.

    Replace the log4j-core dependency with this:

     <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-nop</artifactId>
        <version>1.7.30</version>
     </dependency>
    

I've used this but its not working for me:

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

Yea, that won't work. According the the dependency in your question, you have selected the log4j as the backend. The code snippet above is configuring the java.util.logging logger ...

Look at the javadocs for the log4j classes.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

This worked for me..

static { // Initialized log4j2 Property and disable hibernate log Configurator.initialize("LOGS", "Projects/log4j.properties"); Configurator.setLevel("org.hibernate", org.apache.logging.log4j.Level.OFF); }