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

发布于 2024-09-30 04:56:37 字数 2054 浏览 2 评论 0原文

我正在使用 iBatis 和 spring 框架。 时,我想记录 iBatis 执行的 sql

Employee e = (Employee) getSqlMapClientTemplate().queryForObject("emp_sql", emp);

当我说类似上面的行将在我拥有的 ibatis sql 文件中查找“emp_sql”id 。然后运行“emp_sql”对应的查询。我想记录这个查询。

我有以下 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>

我仍然无法获取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>

我必须使用 p6spy 还是其他东西?或者我可以在 log4j 配置中做些什么来获取 iBatis sql 日志?

I am using iBatis with spring framework. I want to log the sql that iBatis executes when I say something like

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.

I have the following log4j xml properties file.

<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

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

攀登最高峰 2024-10-07 04:56:37

将以下内容添加到您的 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 the following to your log4j configuration (uncomment what you want to see).

# 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
表情可笑 2024-10-07 04:56:37

将其添加到您的 log4j.xml 中

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

Add this in your log4j.xml

<logger name="com.ibatis" additivity="false">
    <level value="debug"/>
    <appender-ref ref="APPENDER"/>
</logger>
彼岸花似海 2024-10-07 04:56:37

如果您使用 Log4j 作为日志框架,则需要将 mybatis 设置为使用 log4j 作为其默认日志工具。您可以通过在 mybatis-config.xml 中设置它来完成此操作,如下所示,

<setting name="logImpl" value="LOG4J"/>

或者如果您不使用 mybatis-config.xml 而只是使用注释,那么您需要

org.apache.ibatis.logging.LogFactory.useLog4JLogging();

在调用任何其他 mybatis 方法之前使用设置默认日志记录实现。 阅读更多...

在您的 log4j.properties< 中使用此配置/strong>,

# 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

如果您使用 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>

使用属性文件或 xml 文件按上述方式配置 log4j 并将其放置在类路径中,以便正常工作。

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,

<setting name="logImpl" value="LOG4J"/>

Or if you are not using mybatis-config.xml and just annotations, then you want to use

org.apache.ibatis.logging.LogFactory.useLog4JLogging();

before invoking any other mybatis methods to set the default logging implementation. Read More...

Use this configuration in your 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,

<?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.

白昼 2024-10-07 04:56:37

将其添加到您的 log4j

<logger name="java.sql" additivity="false">
    <level value="debug" />
    <appender-ref ref="console" /> </logger>

这将打印出 sql 以及输出结果

Add this in your 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

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