简体   繁体   中英

how to configure log4j for Mybatis to print my SQL

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

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.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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