iBatis、spring,如何记录执行的sql?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将以下内容添加到您的 log4j 配置中(取消注释您想要查看的内容)。
Add the following to your log4j configuration (uncomment what you want to see).
将其添加到您的 log4j.xml 中
Add this in your log4j.xml
如果您使用 Log4j 作为日志框架,则需要将 mybatis 设置为使用 log4j 作为其默认日志工具。您可以通过在 mybatis-config.xml 中设置它来完成此操作,如下所示,
或者如果您不使用 mybatis-config.xml 而只是使用注释,那么您需要
在调用任何其他 mybatis 方法之前使用设置默认日志记录实现。 阅读更多...
在您的 log4j.properties< 中使用此配置/strong>,
如果您使用 log4j.xml 配置,请尝试与上述等效的操作,
使用属性文件或 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,
Or if you are not using mybatis-config.xml and just annotations, then you want to use
before invoking any other mybatis methods to set the default logging implementation. Read More...
Use this configuration in your log4j.properties,
If you are using log4j.xml configuration try this equivalent of the above,
Either use properties file or xml file to configure log4j as above and place it in your classpath for this to work correctly.
将其添加到您的 log4j
这将打印出 sql 以及输出结果
Add this in your log4j
This will print out the sql as well as the output results