简体   繁体   English

iBatis,spring,如何记录执行的sql?

[英]iBatis, spring, how to log the sql that is executed?

I am using iBatis with spring framework. 我正在使用带弹簧框架的iBatis。 I want to log the sql that iBatis executes when I say something like 我想记录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. 上面的代码行将在我拥有的ibatis sql文件中查找“emp_sql”id。 And then run the query corresponding to "emp_sql". 然后运行对应于“emp_sql”的查询。 I want to log this query. 我想记录此查询。

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. 我仍然无法获得ibatis执行的sql。 Is there something wrong with the configuration? 配置有问题吗? Should I just say 我应该说

<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? 我是否必须使用p6spy或其他东西? Or is there something that I can do in the log4j configuration to get the iBatis sql logs? 或者我在log4j配置中可以做些什么来获取iBatis sql日志?

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

Add this in your log4j.xml 在log4j.xml中添加它

<logger name="com.ibatis" additivity="false">
    <level value="debug"/>
    <appender-ref ref="APPENDER"/>
</logger>

If you are using Log4j as your logging framework you need to set mybatis to use log4j as its default logging tool. 如果您使用Log4j作为日志记录框架,则需要将mybatis设置为使用log4j作为其默认日志记录工具。 You can do this by setting it in the mybatis-config.xml like this, 您可以通过在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. 在调用任何其他mybatis方法来设置默认日志记录实现之前。 Read More... 阅读更多...

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.xml configuration 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并将其放在类路径中以使其正常工作。

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以及输出结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM