Spring boot jpa不打印sql日志

问题描述 投票:0回答:1

我正在尝试在日志文件中打印sql语句,但是不起作用。

下面是应用程序属性文件:

custom.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/custom_service?useSSL=false
custom.datasource.username=root
custom.datasource.password=paytm@123
custom.datasource.driverClassName=com.mysql.jdbc.Driver
custom.hibernate.dialect=org.hibernate.dialect.MySQLDialect
custom.hibernate.hbm2ddl.auto=none
##show sql statement
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace

Log4j根级别设置为调试模式。请帮助我解决这里的问题。

Log4j配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<DynamicThresholdFilter key="x-debug-enabled" onMatch="ACCEPT" onMismatch="NEUTRAL">
    <KeyValuePair key="true" value="DEBUG"/>
</DynamicThresholdFilter>

<Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="[%level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} requestId: %X{requestId}, customerId: %X{customerId} - %msg%n"></PatternLayout>
    </Console>
    <File name="File" fileName="/var/log/custom-onboarding-service/custom-onboarding-service.log" ignoreExceptions="false">
        <PatternLayout pattern="[%level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} requestId: %X{requestId}, customerId: %X{customerId} - %msg%n"></PatternLayout>
    </File>
    <File name="FileException" fileName="/var/log/custom-onboarding-service/custom-onboarding-service-error.log" ignoreExceptions="false">
        <PatternLayout pattern="[%level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} requestId: %X{requestId}, customerId: %X{customerId} - %msg%n"></PatternLayout>
    </File>
    <RollingFile name="customOnboarding" fileName="/var/log/custom-onboarding-service/custom-onboarding-service-audit.log"
                 filePattern="/var/log/custom-onboarding-service/custom-onboarding-service-audit-%d{yyyy-MM-dd}-%i.log">
        <PatternLayout>
            <Pattern>%m%n</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="250 MB"/>
        </Policies>
    </RollingFile>
</Appenders>

<Loggers>
    <Root level="debug">
        <AppenderRef ref="FileException" level="ERROR"></AppenderRef>
    </Root>
    <Logger name="com.nv.custom" level="debug" additivity="false">
        <AppenderRef ref="File" />
        <AppenderRef ref="FileException" level="ERROR"></AppenderRef>
    </Logger>
    <Logger name="JSON_LOGGER" level="info" additivity="false">
        <AppenderRef ref="customOnboarding" />
    </Logger>
</Loggers>

下面是用于配置spring JPA Config的定制DB Config File:

自定义数据库配置文件:

 @PersistenceContext(unitName = "custom")
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        @Qualifier("customDataSource") DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    factoryBean.setPersistenceUnitName("custom");
    factoryBean.setDataSource(dataSource);
    factoryBean.setPackagesToScan("com.nv.custom.db.mktcustom.entities","com.nv.custom.onboarding.entity");
    Properties jpaProperties = new Properties();
    jpaProperties.put("hibernate.dialect", env.getRequiredProperty("custom.hibernate.dialect"));
    jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("custom.hibernate.hbm2ddl.auto"));
    jpaProperties.put("spring.jpa.properties.hibernate.format_sql", env.getRequiredProperty("custom.hibernate.show_sql"));
    jpaProperties.put("spring.jpa.properties.hibernate.show_sql", env.getRequiredProperty("custom.hibernate.format_sql"));
    factoryBean.setJpaProperties(jpaProperties);
    return factoryBean;
}
spring-boot jpa log4j2
1个回答
0
投票

要在spring-boot中显示sql查询,您需要设置以下属性。

spring.jpa.show-sql=true

您使用自定义属性进行数据库配置。然后在您的自定义配置方法中添加所需的属性。

    custom.hibernate.show_sql=true
    custom.hibernate.format_sql=true
© www.soinside.com 2019 - 2024. All rights reserved.