如何为Mybatis配置log4j以打印我的SQL

如何为Mybatis配置log4j以打印我的SQL

本文介绍了如何为Mybatis配置log4j以打印我的SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是使用SpringMVC + Mybatis + EXTJS4设置的.这是我的配置:

My project is set up with SpringMVC+Mybatis+EXTJS4. This is my configuration:

# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL

# Global logging configuration
log4j.rootLogger=DEBUG,stdout


log4j.logger.org.apache.ibatis=DEBUG,stdout
log4j.logger.java.sql=DEBUG,stdout

#log4j.additivity.com.ibatis = false
#log4j.additivity.java.sql = false

## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

但是它不能在控制台中打印SQL,有人可以帮我吗?非常感谢

but it does not print SQL in Console,can any one help me? thanks lot

推荐答案

Jaradinor的答案可能会起作用.但是,我认为潜在的问题是由于MyBatis LogFactory类是如何实现的.在静态块中,它尝试加载slf4j,然后加载commmons-logging,然后加载log4j.因此,如果您在类路径中有commons-logging,它将使用该路径.由于您使用的是Spring,因此您可能确实有commons-logging.

Jaradinor's answer will probably work. But, I think the underlying issue is due to how the MyBatis LogFactory class is implemented. In a static block, it attempts to load slf4j, then commmons-logging, then log4j. So, if you have commons-logging in your classpath it will use that. Since you are using Spring, you probably do have commons-logging.

是因为这样的问题,我将其移至slf4j.使用 slf4j-log4j12 使slf4j调用log4j.使用 jcl-over-slf4j 将所有Spring(和其他)公共记录日志路由到slf4j(然后到log4j).执行此操作时,请确保从类路径中排除真实​​的"公共日志jar文件-如果使用的是Maven,则可能使用< exclude> .

It's because of issues like this I have moved to slf4j. Use slf4j-log4j12 to have slf4j call to log4j. Use jcl-over-slf4j to route all the Spring (and other) commons-logging to slf4j (and then to log4j). Make sure you exclude the 'real' commons-logging jar file from your classpath when you do this - maybe with an <exclude> if you are using Maven.

这篇关于如何为Mybatis配置log4j以打印我的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:30