Java Spring JDBC 不使用 log4j 记录 SQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792244/
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 JDBC is not logging SQL with log4j
提问by phil swenson
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.
我正在研究弹簧,以便可能切换到弹簧堆栈。我认为很酷的一件事是 spring jdbc 能够记录所有执行的 sql。于是我放入了log4j,设置了一个log4j.properties文件。并且没有sql。
here is the log4j.properties file:
这是 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
here is the output for some really simple insert sql via spring jdbc: http://pastie.org/713189
这是一些非常简单的通过 spring jdbc 插入 sql 的输出:http: //pastie.org/713189
回答by DJ.
From my understanding spring will print out information when you use prepared statement.
根据我的理解,当您使用准备好的语句时,spring 会打印出信息。
See this discussionon Spring's forum.
见这个讨论Spring的论坛。
回答by skaffman
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.
你是如何执行SQL的?Spring 不会神奇地记录发送到数据库的 SQL,您必须通过适当的渠道。
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.
例如,如果您通过 usingJdbcTemplate
直接或通过执行 SQL JdbcDaoSupport
,那么是的,将为某些操作记录 SQL,但仅记录那些涉及直接 SQL 的操作。
If you use Hibernate or prepared statements, then Spring never gets to see the SQL itself, and therefore cannot log it.
如果您使用 Hibernate 或准备好的语句,那么 Spring 永远不会看到 SQL 本身,因此无法记录它。
If you posted some sample code that demonstrated how you were executing your SQL, it would help a lot.
如果您发布了一些示例代码来演示您是如何执行 SQL 的,那将会有很大帮助。
回答by Jason Gritman
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.
您确定这是log4.properties
您的应用程序正在接受的吗?我将log4j.properties
您发布的内容复制到本地机器上的 Spring 应用程序中,除了 JDBC 日志记录之外,我还获得了大量 Spring 调试条目。我在您的输出中没有看到类似的调试条目。
A few probable culprits for your log4j.properties
not getting read correctly are:
您log4j.properties
没有正确阅读的一些可能的罪魁祸首是:
log4j.properties
isn't on your classpath. You can trying doing aclass.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
log4j.properties
不在你的类路径上。你可以尝试class.getResource()
对它做一个,看看它是否找到了它。log4j.properties
你的类路径上还有另一个。- 某些事情使 commons-logging 选择使用不同的记录器。您可以在此处找到有关打开 commons-logging 诊断的说明
回答by serg10
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.
尝试设置这些额外的log4j
记录器。第一个将吐出通过 spring 的 SQL,JdbcTemplate
第二个为您提供 Spring 在准备好的语句上设置的参数值。
<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
.
显然,这只有在您直接或间接使用JdbcTemplate
.
回答by Kroky
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)
这对我有帮助,DEBUG
只是还不够。我正在使用 org.springframework.jdbc-3.0.6.RELEASE.jar 和 log4j-1.2.15 和 slf4j (1.6.4)
For just an SQL (i.e. if you're not interested in bound parameter values) DEBUG
should be enough.
仅对于 SQL(即,如果您对绑定参数值不感兴趣)DEBUG
就足够了。
回答by xerx593
Assuming Spring 3.0.7.RELEASE and log4j 1.2.17, we got the task accomplished via:
假设 Spring 3.0.7.RELEASE 和 log4j 1.2.17,我们通过以下方式完成了任务:
<!-- activates query logging -->
<category name="org.springframework.jdbc.core.JdbcTemplate">
<level value="DEBUG"/>
</category>
<!-- activates parameter substitution logging -->
<category name="org.springframework.jdbc.core.StatementCreatorUtils">
<level value="TRACE"/>
</category>
..in our log4j.xml.
..在我们的 log4j.xml 中。
Replacing <category/>
with <logger/>
works also fine.
<category/>
用<logger/>
作品替换也很好。