Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种方式:
- SLF4J
- Apache Commons Logging
- Log4j 2
- Log4j
- JDK logging
具体选择哪个日志实现由MyBatis的内置日志工厂确定。它会使用最先找到的(按上文列举的顺序查找)。 如果一个都未找到,日志功能就会被禁用。
因此当您的maven工程中引入了SLF4J-API和Log4J, 而您使用Log4j.properties进行日志配置时,便无法打印sql语句到控制台,按照优先级他会先选择使用SLF4J
解决方案
1、添加slf4j-log4j12, pom.xml配置如下:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<!-- 添加该jar, 解决MyBaits 无法打印sql语句到控制台, -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
2、添加mybatis-config.xm, 内容如下:
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>
Log4j配置Mybatis日志
### set log levels ###
log4j.rootLogger = debug, stdout, D, I, W, E
### print log to console ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d %p [%c] - <%m>%n
### create log on lever debug ###
log4j.appender.D = org.apache.log4j.RollingFileAppender
log4j.appender.D.File = D:/logs/debug.log
log4j.appender.D.MaxFileSize = 10240KB
log4j.appender.D.Encoding = UTF-8
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern= %d %p [%c] - <%m>%n
log4j.appender.D.MaxBackupIndex = 10
### create log on lever info ###
log4j.appender.I = org.apache.log4j.RollingFileAppender
log4j.appender.I.File = D:/logs/info.log
log4j.appender.I.MaxFileSize = 10240KB
log4j.appender.I.Encoding = UTF-8
log4j.appender.I.Threshold = INFO
log4j.appender.I.layout = org.apache.log4j.PatternLayout
log4j.appender.I.layout.ConversionPattern= %d %p [%c] - <%m>%n
log4j.appender.I.MaxBackupIndex = 10
### create log on lever warn ###
log4j.appender.W = org.apache.log4j.RollingFileAppender
log4j.appender.W.File = D:/logs/warn.log
log4j.appender.W.MaxFileSize = 10240KB
log4j.appender.W.Encoding = UTF-8
log4j.appender.W.Threshold = WARN
log4j.appender.W.layout = org.apache.log4j.PatternLayout
log4j.appender.W.layout.ConversionPattern= %d %p [%c] - <%m>%n
log4j.appender.W.MaxBackupIndex = 10
### create log on lever error ###
log4j.appender.E = org.apache.log4j.RollingFileAppender
log4j.appender.E.File = D:/logs/error.log
log4j.appender.E.MaxFileSize = 10240KB
log4j.appender.E.Encoding = UTF-8
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern= %d %p [%c] - <%m>%n
log4j.appender.E.MaxBackupIndex = 10
# If programmed properly the most messages would be at DEBUG
# and the least at FATAL.
# Control logging for other open source packages
log4j.logger.net.sf.navigator=ERROR
log4j.logger.net.sf.acegisecurity=WARN
log4j.logger.net.sf.acegisecurity.intercept.event.LoggerListener=WARN
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.apache.struts=WARN
log4j.logger.org.displaytag=ERROR
log4j.logger.org.springframework=WARN
log4j.logger.org.apache.velocity=WARN
# SqlMap logging configuration...
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.db=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=debug
log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=debug
log4j.logger.com.ibatis.common.util.StopWatch=debug
log4j.logger.org.springframework.jdbc.core=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug