Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4, JPA, Turn off console debug messages

I have a basic Spring 4 JPA application. I use all Java configurations (no XML at all). I want to turn off the console debug messages. I have seen many questions on this and tried the solutions but still I see all the messages.

The console messages look like this:

14:58:29.301 [main] DEBUG o.h.loader.entity.plan.EntityLoader....
14:58:29.328 [main] INFO  o.h.tool.hbm2ddl.SchemaUpdate....
....
14:58:29.905 [main] DEBUG o.h.h.i.ast.QueryTranslatorImpl - --- HQL AST ---
    \-[QUERY] Node: 'query'
      \-[SELECT_FROM] Node: 'SELECT_FROM'
      +-[FROM] Node: 'from'
       |  \-[RANGE] Node: 'RANGE'
       |     +-[DOT] Node: '.'
       |     |  +-[IDENT] Node: 'hello'
       |     |  \-[IDENT] Node: 'Customer'
       |     \-[ALIAS] Node: 'generatedAlias0'
    \-[SELECT] Node: 'select'
     \-[IDENT] Node: 'generatedAlias0'
....
Hundreds of lines more....

I tried to set show_sql both in the HibernateJpaVendorAdapter and in the LocalContainerEntityManagerFactoryBean as shown below:

@Bean
  public EntityManagerFactory entityManagerFactory() throws SQLException {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setShowSql(false);

    LocalContainerEntityManagerFactoryBean factory = new
        LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("hello");
    factory.setDataSource(dataSource());

    Properties jpaProperties = new Properties();
    jpaProperties.put( "hibernate.dialect", "org.hibernate.dialect.MySQLDialect" );
    jpaProperties.put( "hibernate.show_sql", false );
    jpaProperties.put( "show_sql", false );
    jpaProperties.put( "hibernate.generate_statistics", false );
    factory.setJpaProperties(jpaProperties);
    factory.afterPropertiesSet();
    return factory.getObject();
  }

Thank you for any ideas on this!

-- Edit -- more info

What I do is create a Spring Starter Project with Spring Tools Suite and just select JPA. I then add MySQL to my pom.xml.

As a test I have a basic Customer and CustomerRepository class and the JPA configuration I noted above.

My Application class:

@Configuration
@EnableAutoConfiguration
public class Application {

  public static void main(String[] args) {

    AbstractApplicationContext context = new
      AnnotationConfigApplicationContext(JPAConfiguration.class);

      CustomerRepository repository = context.getBean(CustomerRepository.class);

      //use the repository.....

      ((AbstractApplicationContext) context).close();
      context.close();
  }
}

So that is it -- a very basic JPA Spring Starter Project created with Spring Tools Suite. If I could figure out how to deal with logging in that I could translate that info to my actual project.

-- Edit -- even more info -- and it's fixed!

OK this is interesting... I changed my Application class and the problem goes away. So using the this Application (vs. the one posted above) and the logging problem is now OK -- anyone who could comment why this works like this?

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

  public static void main(String[] args) {

    ConfigurableApplicationContext context =
      SpringApplication.run(Application.class);

    CustomerRepository repository = context.getBean(CustomerRepository.class);

    //use the repository.....

    context.close();
  }
}

Note the solution provided below by Alan Hay also works great regardless of how I do the Application class!

Note with either way you can still set setJpaProperties as shown my configuration (see Bean above) to control if you want to see Hibernate's SQL, etc.

like image 425
Matt M. Avatar asked May 09 '14 19:05

Matt M.


1 Answers

I encountered the same issue recently. It appears Spring 4 uses the logback library for logging and my app only had a log4j config file. Adding an additional logging configuration file for logback solved the issue.

If you have an existing log4j config file there is a tool to convert this to logback format here:

http://logback.qos.ch/translator/

If not try adding a file named logback.xml to the root of your classpath:

<?xml version="1.0" encoding="UTF-8"?>                                                              
<configuration>
  <appender name="A1" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{dd MMM yyyy HH:mm:ss} %-4r [%t] %-5p %c %x - %m%n</pattern>
    </encoder>
  </appender>
  <logger name="org.springframework" level="ERROR"/>
  <logger name="org.hibernate" level="ERROR"/>
  <root level="DEBUG">
    <appender-ref ref="A1"/>
  </root>
</configuration>
like image 81
Alan Hay Avatar answered Sep 28 '22 01:09

Alan Hay