java Spring Boot JDBC 模板 SQL 日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49068221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot JDBC Template SQL Log
提问by sunleo
I am trying log SQL queries with params for Spring Boot JDBC but it is not printing the details in log.I am using Spring Boot 1.5.8 version.Please help me to solve this.
我正在尝试使用参数为 Spring Boot JDBC 记录 SQL 查询,但它没有在日志中打印详细信息。我使用的是 Spring Boot 1.5.8 版本。请帮我解决这个问题。
application.properties:
应用程序属性:
spring.datasource.url=url
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
logging.level.org.springframework.jdbc.core.JdbcTemplate=debug
spring.datasource.type = com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=2
Repository:
存储库:
@Repository
public class DataRepository {
private static Logger log = LoggerFactory.getLogger(DataRepository.class);
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
public Data findDataObjet() throws Exception {
Map<String, Object> parameters = new HashMap<>();
parameters.put("id1", "mike");
parameters.put("id2", new Long(1));
String sqlString = "select * from table1 where id1 = ":id" and id2 = :id2";
log.info("Query:" + sqlString);//this log is printing
Data extObj = jdbcTemplate.query(sqlString, parameters, (rs) -> {
if (rs != null && rs.next()) {
Data innerObj = new Data();
innerObj.setName(rs.getString("name"));
return innerObj;
} else {
log.info("No records found:"+rs);
return null;
}
});
return extObj;
}
}
logback-spring.xml:
logback-spring.xml:
<appender name="dailyRollingFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logsPath}DATA%d{MMddyyyy}.log
</FileNamePattern>
<maxHistory>4</maxHistory>
</rollingPolicy>
<encoder>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level
%logger{35}-%msg %n</Pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="dailyRollingFileAppender" />
</root>
回答by Chids
Try
尝试
log4j.category.org.springframework.jdbc.core = TRACE
The above statement will print SQL queries with inbound parameters as well.
上面的语句也将打印带有入站参数的 SQL 查询。
Incase you need to log only the query use the following
如果您只需要记录查询,请使用以下内容
log4j.category.org.springframework.jdbc.core = DEBUG
You can enable in your logback file with the following
您可以使用以下命令在您的 logback 文件中启用
<logger name="org.springframework.jdbc.core.JdbcTemplate">
<level value="debug" />
</logger>
<logger name="org.springframework.jdbc.core.StatementCreatorUtils">
<level value="debug" />
</logger>
Update :For Springboot 2.x , it would be
更新:对于 Springboot 2.x,它将是
logging.level.org.springframework.jdbc.core=TRACE
Thanks zhuguowei!
谢谢朱国伟!
回答by Pouriya Zarbafian
Adding the following to your properties file also works:
将以下内容添加到您的属性文件也有效:
logging.level.org.springframework.jdbc.core = TRACE
回答by the hand of NOD
Since Spring-Boot 2.1.x you have to set the property:
logging.level.org.springframework.jdbc.core=TRACE
to log the statement and the parameters.
从 Spring-Boot 2.1.x 开始,您必须设置属性:
logging.level.org.springframework.jdbc.core=TRACE
记录语句和参数。
回答by Elarbi Mohamed Aymen
try those statement in application properties
在应用程序属性中尝试这些语句
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE