Home > database >  How to disable hibernate sql logs in log4j2 with spring boot?
How to disable hibernate sql logs in log4j2 with spring boot?

Time:08-27

We have an application written in spring boot. We are using Hibernate search 6 to fetch data from elasticsearch. We have configured log4j2 as below:

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" packages="net.xxxx.logging">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
        </Console>
        <Console name="ConsoleDev" target="SYSTEM_OUT">
          <PatternLayout pattern="%d{HH:mm:ss} [%t]
      %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} - %msg%n"/>
        </Console>
      </Appenders>
      <Loggers>
          <Logger name="com.zaxxer.hikari" level="${env:LOG_LEVEL_HIKARI:-INFO}" additivity="false" >
              <AppenderRef ref="${env:LOG_CONSOLE:-Console}" />
          </Logger>
          <Logger name="org.hibernate" level="INFO" additivity="false" >
              <AppenderRef ref="${env:LOG_CONSOLE:-Console}" />
          </Logger>
          <Logger name="org.hibernate.search.fulltext_query" level="INFO" additivity="false" >
              <AppenderRef ref="${env:LOG_CONSOLE:-Console}" />
          </Logger>
          <Logger name="org.springframework" level="${env:LOG_LEVEL_SPRING:-INFO}" additivity="false" >
              <AppenderRef ref="${env:LOG_CONSOLE:-Console}" />
          </Logger>
          <Logger name="org.springframework.ldap" level="${env:LOG_LEVEL_LDAP:-INFO}" additivity="false" >
              <AppenderRef ref="${env:LOG_CONSOLE:-Console}" />
          </Logger>
          <Root level="INFO" includeLocation="false">
              <AppenderRef ref="${env:LOG_CONSOLE:-Console}" />
          </Root>
      </Loggers>
</Configuration>

application.yaml:

spring:
  profiles: development
  liquibase.enabled: false
  jpa:
    show-sql: true
    properties:
      hibernate:
        format_sql: false
        dialect: org.hibernate.dialect.SQLServer2012Dialect
        generate_statistics: false
        search:
          enabled: true
          backend:
            type: elasticsearch
            dynamic_mapping: true
            uris: ${ELASTIC_SEARCH_URL}
            aws:
              region: us-west-2
              signing.enabled: true

We want to show hibernet sql queries logs show in debug mode only(in local environment) not in production build environment.

We tried by setting show_sql= false in application.yaml file but it disable logs on local and prod both environment. We want to enable to log sql queries in local environment.

CodePudding user response:

You can use Profiles from spring boot spring profiles. The best way to get what you expected.

CodePudding user response:

You can use Spring Profile for segregation between your Local and Production environment. Create two different copies of application.yml file like - application-dev.yml and application-prod.yml. Here in application-dev.yml you can enable the hibernate logs with show_sql= true while in another file application-prod.yml you can disable it show_sql= false.

You will have to create an environment variable SPRING_PROFILES_ACTIVE for picking files depending on your instance. Suppose your instance is development then you need to set this variable to dev otherwise it would be prod.

To switch profiles you can use one of the following options:

JVM property : -Dspring.profiles.active=dev
Command line switch: --spring.profiles.active=dev
Property file : spring.profiles.active=dev

  • Related