Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 454
sence Avatar asked Sep 01 '11 08:09

sence


People also ask

Does MyBatis use JDBC?

Background information. MyBatis is a persistence framework for Java that supports custom SQL statements, stored procedures, and advanced mappings. MyBatis eliminates the need to use JDBC code, manually configure parameters, and retrieve result sets.

What is TypeHandler in MyBatis?

typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.


2 Answers

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.

like image 160
AngerClown Avatar answered Oct 08 '22 13:10

AngerClown


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.

like image 33
Jarandinor Avatar answered Oct 08 '22 12:10

Jarandinor