Spring JDBC 不使用 log4j 记录 SQL

发布于 2024-08-12 17:45:19 字数 635 浏览 5 评论 0原文

我正在研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独守阴晴ぅ圆缺 2024-08-19 17:45:19

尝试设置这些额外的 log4j 记录器。第一个将输出通过 spring 的 JdbcTemplate 的 SQL,第二个为您提供 Spring 在准备好的语句上设置的参数值。

<logger name="org.springframework.jdbc.core.JdbcTemplate">
  <level value="debug" />
</logger>

<logger name="org.springframework.jdbc.core.StatementCreatorUtils">
  <level value="debug" />
</logger>

显然,只有当您使用 JdbcTemplate 直接或间接执行 SQL 时,这才有效。

Try setting these additional log4j loggers. The first will spit out the SQL that passes through spring's JdbcTemplate, the second gives you parameter values that Spring sets on prepared statements.

<logger name="org.springframework.jdbc.core.JdbcTemplate">
  <level value="debug" />
</logger>

<logger name="org.springframework.jdbc.core.StatementCreatorUtils">
  <level value="debug" />
</logger>

Clearly this is only going to work if you're directly or indirectly executing SQL using JdbcTemplate.

玉环 2024-08-19 17:45:19

您确定这是您的应用程序正在获取的 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 the log4j.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 a class.getResource() on it to see if it's finding it at all.
  • There's another log4j.properties on your classpath.
  • Something is making commons-logging choose to use a different logger. You can find instructions for turning on the commons-logging diagnositics here
孤星 2024-08-19 17:45:19

尝试

log4j.category.org.springframework.jdbc.core = TRACE

这对我有帮助,DEBUG还不够。我正在将 org.springframework.jdbc-3.0.6.RELEASE.jar 与 log4j-1.2.15 和 slf4j (1.6.4) 一起使用,

仅用于 SQL(即,如果您对绑定参数值不感兴趣)DEBUG 应该足够了。

Try

log4j.category.org.springframework.jdbc.core = TRACE

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.

微凉 2024-08-19 17:45:19

据我了解,当您使用准备好的语句时,spring 会打印出信息。

请参阅 Spring 论坛上的此讨论

From my understanding spring will print out information when you use prepared statement.

See this discussion on Spring's forum.

那一片橙海, 2024-08-19 17:45:19

你是如何执行 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 via JdbcDaoSupport, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文