Spring JDBC 不使用 log4j 记录 SQL
我正在研究 spring 是否有可能切换到 spring stack。我认为很酷的一件事是 spring jdbc 能够记录所有执行的 sql。所以我放入log4j,设置一个log4j.properties文件。并且没有sql。
这是 log4j.properties 文件:
log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.Patt ernLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
log4j.category.org.springframework.jdbc.core=DEBUG
这是通过 spring jdbc 进行一些非常简单的插入 sql 的输出:http://pastie。 org/713189
I'm researching spring for a possible switch to a spring stack. One of the things that I thought was cool was the ability for spring jdbc to log all the executed sql. So I put in log4j, set up a log4j.properties file. and no sql.
here is the log4j.properties file:
log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.Patt ernLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
log4j.category.org.springframework.jdbc.core=DEBUG
here is the output for some really simple insert sql via spring jdbc: http://pastie.org/713189
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试设置这些额外的
log4j
记录器。第一个将输出通过 spring 的 JdbcTemplate 的 SQL,第二个为您提供 Spring 在准备好的语句上设置的参数值。显然,只有当您使用 JdbcTemplate 直接或间接执行 SQL 时,这才有效。
Try setting these additional
log4j
loggers. The first will spit out the SQL that passes through spring'sJdbcTemplate
, the second gives you parameter values that Spring sets on prepared statements.Clearly this is only going to work if you're directly or indirectly executing SQL using
JdbcTemplate
.您确定这是您的应用程序正在获取的
log4.properties
吗?我将您发布的 log4j.properties 复制到本地计算机上的 Spring 应用程序中,除了 JDBC 日志记录之外,我还获得了大量 Spring 调试条目。我在您的输出中没有看到类似的调试条目。导致您的
log4j.properties
无法正确读取的一些可能的原因是:log4j.properties
不在您的类路径中。您可以尝试对其执行class.getResource()
来查看它是否找到它。log4j.properties
。Are you sure this is the
log4.properties
that your application is picking up? I copied thelog4j.properties
you posted into a Spring application on my local machine, and I got a ton of Spring debug entries in addition to the JDBC logging. I don't see debug entries like that in your output.A few probable culprits for your
log4j.properties
not getting read correctly are:log4j.properties
isn't on your classpath. You can trying doing aclass.getResource()
on it to see if it's finding it at all.log4j.properties
on your classpath.尝试
这对我有帮助,
DEBUG
还不够。我正在将 org.springframework.jdbc-3.0.6.RELEASE.jar 与 log4j-1.2.15 和 slf4j (1.6.4) 一起使用,仅用于 SQL(即,如果您对绑定参数值不感兴趣)
DEBUG
应该足够了。Try
This helped me,
DEBUG
just wasn't enough. I am using org.springframework.jdbc-3.0.6.RELEASE.jar with log4j-1.2.15 and slf4j (1.6.4)For just an SQL (i.e. if you're not interested in bound parameter values)
DEBUG
should be enough.据我了解,当您使用准备好的语句时,spring 会打印出信息。
请参阅 Spring 论坛上的此讨论。
From my understanding spring will print out information when you use prepared statement.
See this discussion on Spring's forum.
你是如何执行 SQL 的? Spring 不会神奇地记录发送到数据库的 SQL,您必须通过适当的渠道。
例如,如果您使用
JdbcTemplate
直接或通过JdbcDaoSupport
执行 SQL,那么是的,某些操作的 SQL 将会被记录,但只有那些涉及直接的操作SQL。如果您使用 Hibernate 或准备好的语句,那么 Spring 永远不会看到 SQL 本身,因此无法记录它。
如果您发布了一些示例代码来演示如何执行 SQL,它将有很大帮助。
How are you executing the SQL? Spring won't magically log SQL that gets sent to the database, you have to go through the appropriate channels.
For example, if you execute SQL by using
JdbcTemplate
, either directly or viaJdbcDaoSupport
, then yes, the SQL will be logged for some operations, but only those operations that involve direct SQL.If you use Hibernate or prepared statements, then Spring never gets to see the SQL itself, and therefore cannot log it.
If you posted some sample code that demonstrated how you were executing your SQL, it would help a lot.