0

I am using the below logback configuration. My project is based on spring data jpa 2.0.13.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </encoder>
    </appender>
    <logger name="org.hibernate.SQL" level="debug" additivity="false">
        <appender-ref ref="STDOUT"/>
    </logger>
    <logger name="org.hibernate.type.descriptor.sql" level="trace" additivity="false">
        <appender-ref ref="STDOUT"/>
    </logger>
    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

I can not see any CRUD sql log like select statement printed in console. When I set the hibernate.show_sql to true, it will work, but I disabled it since I prefer to use logback. I can see the log from spring and my business code, why the hibernate not work?

Below is my maven tree.

[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ storybook ---
[INFO] com.demo.test:storybook:war:2.5.0
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] +- log4j:apache-log4j-extras:jar:1.2.17:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
[INFO] +- javax.jms:jms:jar:1.1:compile
[INFO] +- xerces:xercesImpl:jar:2.10.0:compile
[INFO] |  \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] +- io.springfox:springfox-swagger2:jar:2.5.0:compile
[INFO] |  +- io.swagger:swagger-annotations:jar:1.5.9:compile
[INFO] |  +- io.swagger:swagger-models:jar:1.5.9:compile
[INFO] |  +- io.springfox:springfox-spi:jar:2.5.0:compile
[INFO] |  |  \- io.springfox:springfox-core:jar:2.5.0:compile
[INFO] |  +- io.springfox:springfox-schema:jar:2.5.0:compile
[INFO] |  +- io.springfox:springfox-swagger-common:jar:2.5.0:compile
[INFO] |  +- io.springfox:springfox-spring-web:jar:2.5.0:compile
[INFO] |  +- com.google.guava:guava:jar:20.0:compile
[INFO] |  +- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] |  +- org.springframework.plugin:spring-plugin-metadata:jar:1.2.0.RELEASE:compile
[INFO] |  \- org.mapstruct:mapstruct:jar:1.0.0.Final:compile
[INFO] +- org.zenframework.z8.dependencies.commons:ojdbc6:jar:2.0:compile
[INFO] +- io.springfox:springfox-swagger-ui:jar:2.5.0:compile
[INFO] +- javax.mail:mail:jar:1.4:compile
[INFO] +- concurrent:concurrent:jar:1.3.4:compile
[INFO] +- com.jcraft:jsch:jar:0.1.54:compile
[INFO] +- commons-digester:commons-digester:jar:2.1:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.apache.xmlbeans:xmlbeans:jar:2.6.0:compile
[INFO] |  \- stax:stax-api:jar:1.0.1:compile
[INFO] +- commons-fileupload:commons-fileupload:jar:1.3.3:compile
[INFO] +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- commons-beanutils:commons-beanutils:jar:1.9.3:compile
[INFO] |  \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- commons-codec:commons-codec:jar:1.11:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
[INFO] +- com.google.code.gson:gson:jar:2.8.5:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.13:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- javax.activation:activation:jar:1.1.1:compile
[INFO] +- org.springframework:spring-webmvc:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:5.0.12.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:5.0.12.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:5.0.12.RELEASE:compile
[INFO] +- com.h2database:h2:jar:1.4.197:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.2.17.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:5.2.17.Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  \- org.jboss:jandex:jar:2.0.3.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.22.0-GA:compile
[INFO] |  +- net.bytebuddy:byte-buddy:jar:1.7.11:compile
[INFO] |  \- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.1.Final:compile
[INFO] \- org.springframework.data:spring-data-jpa:jar:2.0.13.RELEASE:compile
[INFO]    +- org.springframework.data:spring-data-commons:jar:2.0.13.RELEASE:compile
[INFO]    +- org.springframework:spring-orm:jar:5.0.12.RELEASE:compile
[INFO]    |  \- org.springframework:spring-jdbc:jar:5.0.12.RELEASE:compile
[INFO]    +- org.springframework:spring-tx:jar:5.0.12.RELEASE:compile
[INFO]    \- org.aspectj:aspectjrt:jar:1.8.13:compile

Below is my dbconfig for spring.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "h2_entityManagerFactory",
  basePackages = { "com.demo.test.repository" },
        transactionManagerRef = "h2_manager"

)
public class H2DBConfig {

  @Primary
  @Bean(name = "h2_dataSource")
  public DataSource dataSource(){
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("org.h2.Driver");

      dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
      dataSource.setUsername("");
      dataSource.setPassword("");
      return dataSource;
  }

  @Primary
  @Bean(name = "h2_entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean h2EntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em
              = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] { "com.demo.test.entity" });
      em.setPersistenceUnitName("h2_unit");
      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalProperties());

      return em;
  }

  Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
//        properties.setProperty("hibernate.show_sql", "true");
        return properties;
    }


    @Bean(name = "h2_manager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("h2_entityManagerFactory") EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}
Peter Šály
  • 2,848
  • 2
  • 12
  • 26
liam xu
  • 2,892
  • 10
  • 42
  • 65
  • You may have to configure Hibernate to log via SLF4J: https://stackoverflow.com/questions/11639997/how-do-you-configure-logging-in-hibernate-4-to-use-slf4j Otherwise it might use JBoss Logging instead (and since you also have Log4J, it in turn might use that) – Thilo Mar 29 '19 at 06:49
  • Thanks I will look at it, I import log4j since there is a legacy framework in our company use log4j directly. – liam xu Mar 29 '19 at 07:01
  • For that, you should use log4j-over-slf4j instead. It provides the log4j API, but sends all messages into slf4j. – Thilo Mar 30 '19 at 02:04

0 Answers0