如何使用 log4j2 在日志文件中记录 spring JdbcTemplate sql 查询和数据库响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22555284/
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
How to log spring JdbcTemplate sql queries and DB response in log file using log4j2
提问by vivek
I am using spring org.springframework.jdbc.core.JdbcTemplate and org.springframework.jdbc.core.simple.SimpleJdbcCall for my database queries. I am using log4j2.xml for logging. I want to log all my sql queries and their DB result in db.log file.
我正在使用 spring org.springframework.jdbc.core.JdbcTemplate 和 org.springframework.jdbc.core.simple.SimpleJdbcCall 进行我的数据库查询。我正在使用 log4j2.xml 进行日志记录。我想在 db.log 文件中记录我所有的 sql 查询及其数据库结果。
I have tried to used following logger in my log4j2.xml file but it did not log anything in db.log file. I tried level="TRACE" and level="debug" but both not working.
我试图在我的 log4j2.xml 文件中使用以下记录器,但它没有在 db.log 文件中记录任何内容。我试过 level="TRACE" 和 level="debug" 但都不起作用。
<RollingFile name="db" fileName="${sys:catalina.home}/logs/db.log"
filePattern="${sys:catalina.home}/logs/$${date:yyyy-MM-dd}/db-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout
pattern="%d{dd/MM/yyyy HH:mm:ss,SSS} [%X{cartID}] [%X{sessionId}] [%p] [%t] [%c] (%F:%L) - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.jdbc.core.JdbcTemplate" level="TRACE" additivity="false">
<Appender-Ref ref="db" />
</Logger>
In our java classes we are using following sql
在我们的 java 类中,我们使用以下 sql
String sQuery = "select count(*) from impersonation_requests where ir_eid = ? and ir_tmp_userid = ?";
String value = template
.queryForObject(sQuery, new Object[] { passwordInfo.getEid(),
passwordInfo.getUserId() }, String.class);
Here var template is instance variable of org.springframework.jdbc.core.JdbcTemplate
这里 var 模板是 org.springframework.jdbc.core.JdbcTemplate 的实例变量
I want to see sQuery and value entries in my db.log file. Can we achieve this using JdbcTemplate or I need to implement logger in all my DAO classes and log sQuery and values in each class where I am using JdbcTemplate. I want to avoid this approach. Please suggest.
我想在我的 db.log 文件中查看 sQuery 和值条目。我们可以使用 JdbcTemplate 来实现这一点,或者我需要在我所有的 DAO 类中实现记录器,并在我使用 JdbcTemplate 的每个类中记录 sQuery 和值。我想避免这种方法。请建议。
回答by Artem Bilan
I'd say name="org.springframework.jdbc.core.JdbcTemplate"
is very 'strict'. Try this category:
我会说name="org.springframework.jdbc.core.JdbcTemplate"
非常“严格”。试试这个类别:
org.springframework.jdbc
回答by Gomti
<Logger name="org.springframework.jdbc.core.JdbcTemplate" level="TRACE" additivity="false">
<Appender-Ref ref="db" />
</Logger>
This will definitely work. debug and trace both level will work. Only thing is JDBCTemplate is using common-logging API to log. And if you are using log4j in your application, you will have to add common logging bridge for the same.
这肯定会奏效。调试和跟踪两个级别都可以工作。唯一的问题是 JDBCTemplate 使用通用日志记录 API 来记录。如果您在应用程序中使用 log4j,则必须为其添加公共日志记录桥。
Add following in your pom.xml
在 pom.xml 中添加以下内容
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.0-rc1</version>
</dependency>