how to configure log4j for Mybatis to print my SQL

删除回忆录丶 提交于 2021-02-06 08:47:52

问题


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

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


回答1:


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.

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.




回答2:


I encountered a similar problem. The following code helped me (i've used it in the mybatis class configuration):

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

May cause problems become other logging system in your classpath.




回答3:


You need to put slf4j-api-xxx.jar, slf4j-log4j12-xxx.jar, log4j-over-slf4j-xxx.jar, log4j-xxx.jar (replace xxx by the version, for example 1.6.3) in your classpath and in your src package, put the log4j.properties (see below).

log4j.debug=true

log4j.rootCategory=DEBUG

## uncoment when run in production ##
#log4j.threshold=INFO

# logger error
log4j.logger.br.danilo.psc.exceptions=ERROR, psc-error
log4j.appender.psc-error = org.apache.log4j.RollingFileAppender
log4j.appender.psc-error.File=c:\\log-psc\\error\\log-error.log
log4j.appender.psc-error.MaxFileSize=1200KB
log4j.appender.psc-error.MaxBackupIndex=40
log4j.appender.psc-error.layout=org.apache.log4j.PatternLayout
log4j.appender.psc-error.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss,SSS} [%-5p] %m %n
# end logger error

# logger debug
log4j.logger.br.danilo.psc=DEBUG, psc-debug
log4j.appender.psc-debug = org.apache.log4j.ConsoleAppender
log4j.appender.psc-debug.layout=org.apache.log4j.PatternLayout
log4j.appender.psc-debug.layout.ConversionPattern=[%d{HH:mm:ss}] %-5p %c{3} %x - %m%n
# end logger debug

### mybatis loggers ###
log4j.logger.com.ibatis=DEBUG, psc-debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG, psc-debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG, psc-debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG, psc-debug

# sql loggers
log4j.logger.java.sql.Connection=DEBUG, psc-debug
log4j.logger.java.sql.Statement=DEBUG, psc-debug
log4j.logger.java.sql.PreparedStatement=DEBUG, psc-debug
log4j.logger.java.sql.ResultSet=DEBUG, psc-debug

with this .properties, you log all steps in mybatis and sql like open connection, fetch results, close connections and etc.

cheers!



来源:https://stackoverflow.com/questions/7267834/how-to-configure-log4j-for-mybatis-to-print-my-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!