Java iBatis,spring,如何记录执行的sql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4082834/
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
iBatis, spring, how to log the sql that is executed?
提问by aadidasu
I am using iBatis with spring framework. I want to log the sql that iBatis executes when I say something like
我正在使用带有 spring 框架的 iBatis。我想记录 iBatis 执行的 sql,当我说类似的话
Employee e = (Employee) getSqlMapClientTemplate().queryForObject("emp_sql", emp);
The above line will look for "emp_sql" id in the ibatis sql file that I have. And then run the query corresponding to "emp_sql". I want to log this query.
上面的行将在我拥有的 ibatis sql 文件中查找“emp_sql”ID。然后运行“emp_sql”对应的查询。我想记录这个查询。
I have the following log4j xml properties file.
我有以下 log4j xml 属性文件。
<appender name="sqlLogAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/disk1/logs/sql.log"/>
<param name="datePattern" value="'-'yyyy-MM-dd'.txt'"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m %n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
</filter>
</appender>
<logger name="log4j.logger.com.ibatis">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
<logger name="log4j.logger.java.sql.Connection">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
<logger name="log4j.logger.java.sql.PreparedStatement">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
I still cannot get the sql that the ibatis executed. Is there something wrong with the configuration? Should I just say
我仍然无法获得ibatis执行的sql。配置有问题吗?我应该说
<appender name="sqlLogAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/disk1/logs/sql.log"/>
<param name="datePattern" value="'-'yyyy-MM-dd'.txt'"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m %n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
</filter>
</appender>
<logger name="log4j.logger.java.sql">
<level value="DEBUG"/>
<appender-ref ref="sqlLogAppender"/>
</logger>
Do I have to use p6spy or something else? Or is there something that I can do in the log4j configuration to get the iBatis sql logs?
我必须使用 p6spy 或其他什么吗?或者我可以在log4j配置中做些什么来获取iBatis sql日志?
采纳答案by DwB
Add the following to your log4j configuration (uncomment what you want to see).
将以下内容添加到您的 log4j 配置中(取消注释您想查看的内容)。
# SqlMap logging configuration. #log4j.logger.com.ibatis=DEBUG #log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG #log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG #log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG #log4j.logger.java.sql=DEBUG #log4j.logger.java.sql.Connection=DEBUG #log4j.logger.java.sql.Statement=DEBUG #log4j.logger.java.sql.PreparedStatement=DEBUG #log4j.logger.java.sql.ResultSet=DEBUG
回答by vsingh
Add this in your log4j
将此添加到您的 log4j 中
<logger name="java.sql" additivity="false">
<level value="debug" />
<appender-ref ref="console" /> </logger>
This will print out the sql as well as the output results
这将打印出 sql 以及输出结果
回答by David García González
Add this in your log4j.xml
将此添加到您的 log4j.xml 中
<logger name="com.ibatis" additivity="false">
<level value="debug"/>
<appender-ref ref="APPENDER"/>
</logger>
回答by Lucky
If you are using Log4j as your logging framework you need to set mybatis to use log4j as its default logging tool. You can do this by setting it in the mybatis-config.xml like this,
如果您使用 Log4j 作为日志框架,则需要将 mybatis 设置为使用 log4j 作为其默认日志工具。你可以通过像这样在 mybatis-config.xml 中设置它来做到这一点,
<setting name="logImpl" value="LOG4J"/>
Or if you are not using mybatis-config.xml and just annotations, then you want to use
或者,如果您不使用 mybatis-config.xml 而只是注释,那么您想使用
org.apache.ibatis.logging.LogFactory.useLog4JLogging();
before invoking any other mybatis methods to set the default logging implementation. Read More...
在调用任何其他 mybatis 方法来设置默认日志记录实现之前。阅读更多...
Use this configuration in your log4j.properties,
在您的log4j.properties 中使用此配置,
# Global logging configuration
log4j.rootLogger=INFO, stdout
# MyBatis mapper interfaces logging configuration...
log4j.logger.com.sample.mappers=DEBUG
# SqlMap logging configuration.
log4j.logger.org.mybatis.spring=DEBUG
log4j.logger.org.apache.ibatis=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%p] %c - %m%n
If you are using log4j.xmlconfiguration try this equivalent of the above,
如果您使用的是log4j.xml配置,请尝试与上述等效,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%p] %c{1} - %m%n"/>
</layout>
</appender>
<logger name="org.mybatis.spring" additivity="false">
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</logger>
<logger name="com.sample.mappers">
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</logger>
<!-- Other custom 3rd party logger configs -->
<root>
<priority value ="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
Either use properties file or xml file to configure log4j as above and place it in your classpath for this to work correctly.
使用属性文件或 xml 文件如上所述配置 log4j 并将其放置在您的类路径中以使其正常工作。