log4j2打印JdbcTemplate的sql及其参数

1 看源码

  • org.springframework.jdbc.core包下面的所有类,输出debug级别的日志

  • org.springframework.jdbc.core.JdbcTemplate 类,输出debug级别的日志

  • org.springframework.jdbc.core.StatementCreatorUtils类,输出trace级别的日志

直接看源码

/org/springframework/jdbc/core/JdbcTemplate.class

public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
    // 略
    @Nullable
    public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action) throws DataAccessException {
        Assert.notNull(psc, "PreparedStatementCreator must not be null");
        Assert.notNull(action, "Callback object must not be null");
        if (this.logger.isDebugEnabled()) {
            String sql = getSql(psc);
            // 可以看到,debug 级别的sql
            this.logger.debug("Executing prepared SQL statement" + (sql != null ? " [" + sql + "]" : ""));
        }
        // 略
    }
    // 略
}

/org/springframework/jdbc/core/StatementCreatorUtils.class

public abstract class StatementCreatorUtils {
    // 略
    // 设置 通配符 ? 的值
    private static void setParameterValueInternal(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName, @Nullable Integer scale, @Nullable Object inValue) throws SQLException {
        // 略
        // 判断Trace 级别的日志是否开启
        if (logger.isTraceEnabled()) {
            // 打印 设置通配符 ? 的值
            logger.trace("Setting SQL statement parameter value: column index " + paramIndex + ", parameter value [" + inValueToUse + "], value class [" + (inValueToUse != null ? inValueToUse.getClass().getName() : "null") + "], SQL type " + (sqlTypeToUse == -2147483648 ? "unknown" : Integer.toString(sqlTypeToUse)));
        }
        // 略
    }
    // 略
}

开源的程序的话自己看下源码,问题说不定就解决了,哈哈蛤!!!


2 log4j2 配置文件

控制台打印sql及其参数,同时保存到文件

log4j2-spring.xml




<configuration monitorInterval="5">

    
    <Properties>
        
        
        <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
        
        <property name="FILE_PATH" value="/home/yoyo/idea-workspace/springboot-log4j2-jdbc-demo/logs" />
        <property name="FILE_NAME" value="springboot-log4j2-jdbc-demo" />
    Properties>

    <appenders>

        
        <console name="Console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="${LOG_PATTERN}"/>
            
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
        console>

        
        <RollingFile name="RollingFileSql" fileName="${FILE_PATH}/sql.log" filePattern="${FILE_PATH}/${FILE_NAME}-DEBUG-%d{yyyy-MM-dd}_%i.log.gz">
            
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                
                <TimeBasedTriggeringPolicy interval="1"/>
                <SizeBasedTriggeringPolicy size="100MB"/>
            Policies>
            
            <DefaultRolloverStrategy max="15"/>
        RollingFile>

    appenders>

    
    
    <loggers>

        
        <Logger name="org.springframework.jdbc.core" level="debug">
            <AppenderRef ref="RollingFileSql"/>
        Logger>
        <Logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="trace">
            <AppenderRef ref="RollingFileSql"/>
        Logger>

        
        <root level="info">
            <appender-ref ref="Console"/>
        root>

    logers>

configuration>

3 打印结果

22:51:33.446 [main] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL query
// sql 
22:51:33.450 [main] DEBUG org.springframework.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT * FROM `user` where `id`=?]
// 参数
22:51:33.462 [main] TRACE org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [1], value class [java.lang.Integer], SQL type unknown
// 字段 / 属性的 类型
22:51:33.475 [main] DEBUG org.springframework.jdbc.core.BeanPropertyRowMapper - Mapping column 'id' to property 'id' of type 'java.lang.Integer'
22:51:33.475 [main] DEBUG org.springframework.jdbc.core.BeanPropertyRowMapper - Mapping column 'userName' to property 'userName' of type 'java.lang.String'
22:51:33.475 [main] DEBUG org.springframework.jdbc.core.BeanPropertyRowMapper - Mapping column 'password' to property 'password' of type 'java.lang.String'

控制台成功打印sql日志,sql.log 文件中也记录了sql日志,OK

你可能感兴趣的:(springboot)