集成slf4j和logback 很简单,导入几个jar就行了,参考这篇文章https://www.cnblogs.com/ZSG-DoBestMe/p/5120502.html
问题1、使用jdbcTemplate如何在日志中打印sql呢?网上很多例子是要加上 这句话就可以了:
代码语言:javascript复制 <logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="DEBUG" >
测试后发现依然无法输出sql。
继续查看spring-jdbc源码 发现有:
代码语言:javascript复制 private static void setParameterValueInternal(PreparedStatement ps, int paramIndex, int sqlType, String typeName, Integer scale, Object inValue)
throws SQLException
{
String typeNameToUse = typeName;
int sqlTypeToUse = sqlType;
Object inValueToUse = inValue;
if (inValue instanceof SqlParameterValue)
{
SqlParameterValue parameterValue = (SqlParameterValue)inValue;
if (logger.isDebugEnabled())
logger.debug((new StringBuilder("Overriding typeinfo with runtime info from SqlParameterValue: column index ")).append(paramIndex).append(", SQL type ").append(parameterValue.getSqlType()).append(", Type name ").append(parameterValue.getTypeName()).toString());
if (parameterValue.getSqlType() != 0x80000000)
sqlTypeToUse = parameterValue.getSqlType();
if (parameterValue.getTypeName() != null)
typeNameToUse = parameterValue.getTypeName();
inValueToUse = parameterValue.getValue();
}
if (logger.isTraceEnabled())
logger.trace((new StringBuilder("Setting SQL statement parameter value: column index ")).append(paramIndex).append(", parameter value [").append(inValueToUse).append("], value class [").append(inValueToUse == null ? "null" : inValueToUse.getClass().getName()).append("], SQL type ").append(sqlTypeToUse != 0x80000000 ? Integer.toString(sqlTypeToUse) : "unknown").toString());
if (inValueToUse == null)
setNull(ps, paramIndex, sqlTypeToUse, typeNameToUse);
else
setValue(ps, paramIndex, sqlTypeToUse, typeNameToUse, scale, inValueToUse);
}
注意:logger.isDebugEnabled(),鉴于此我们需要在logback配置文件中增加如下配置,增加debug=true 配置。
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" debug="true">
测试发现依然无法打印sql,对比后发现原因是spring-jdbc引用的是commons-logging的日志包,对应的如果我们需要使用logback需要使用jcl-over-slf4j.jar替换commons-logging
替换后效果:
代码语言:javascript复制11:22:01.565 DEBUG [DataSourceUtils.java:332:doReleaseConnection] [http-8080-1 ] - Returning JDBC Connection to DataSource
11:22:01.565 DEBUG [JdbcTemplate.java:434:query] [http-8080-1 ] - Executing SQL query [select areaid as id,area as name,parentId as pid,CASE WHEN type = 0 THEN 1 WHEN type =1 THEN 2 WHEN type =2 THEN 3 WHEN type =3 THEN 4 END AS level from xxxx where type=0 and addresstype=1 order by areaid desc]
11:22:01.567 DEBUG [DataSourceUtils.java:110:doGetConnection] [http-8080-1 ] - Fetching JDBC Connection from DataSource
11:22:01.601 DEBUG [DataSourceUtils.java:332:doReleaseConnection] [http-8080-1 ] - Returning JDBC Connection to DataSource
11:22:01.710 DEBUG [AbstractConverter.java:336:setDefaultValue] [http-8080-1 ] - Setting default value: false
11:22:01.710 DEBUG [AbstractConverter.java:127:convert] [http-8080-1 ] - Converting 'Boolean' value 'false' to type 'Boolean'
11:22:01.710 DEBUG [AbstractConverter.java:149:convert] [http-8080-1 ] - No conversion required, value is already a Boolean
11:22:01.713 DEBUG [AbstractConverter.java:336:setDefaultValue] [http-8080-1 ] - Setting default value: 0
11:22:01.713 DEBUG [AbstractConverter.java:127:convert] [http-8080-1 ] - Converting 'Integer' value '0' to type 'Byte'