Spring 4,JPA,关闭控制台调试消息 [英] Spring 4, JPA, Turn off console debug messages

查看:654
本文介绍了Spring 4,JPA,关闭控制台调试消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的Spring 4 JPA应用程序。我使用所有的Java配置(没有XML)。我要关闭控制台调试消息。我已经看到了很多问题,并尝试了解决方案,但仍然看到所有的消息。

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

我试图在HibernateJpaVendorAdapter中设置show_sql,在LocalContainerEntityManagerFactoryBean中如下所示:

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

我所做的是使用Spring Tools Suite创建一个Spring Starter Project只需选择JPA。然后我添加MySQL到我的pom.xml。

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.

作为一个测试,我有一个基本的Customer和CustomerRepository类和上面提到的JPA配置。

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

我的应用程序类:

@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();
  }
}

这是一个非常基本的JPA Spring使用Spring Tools Suite创建的Starter项目。

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.

- 编辑 - 更多信息 - 如果我能找到如何处理登录,我可以翻译该信息到我的实际项目。它是固定的!

OK这很有趣...
我改变了我的Application类,问题消失了。
所以使用这个应用程序(与上面发布的)和日志记录问题现在可以 - 任何人谁可以评论为什么这样工作。

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();
  }
}

请注意,Alan Hay提供的解决方案无论我如何做Application类!

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

注意,你仍然可以按照我的配置(见上面的Bean)来设置setJpaProperties来控制如果你想看到Hibernate的SQL等。

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.

推荐答案

我最近遇到同样的问题。它看起来Spring 4使用logback库进行日志记录,我的应用程序只有一个log4j配置文件。

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.

如果你有一个现有的log4j配置文件,有一个工具可以将其转换为logback格式:

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

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

如果不尝试向类路径的根目录添加一个名为logback.xml的文件:

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>

这篇关于Spring 4,JPA,关闭控制台调试消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆