1、maven 工程引入依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2、mybatis 的全局配置文件中加上下列配置
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
3、log4j.properties
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
#log4j.rootLogger=INFO, stdout, logfile
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
#log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#log4j.appender.logfile.File=system.log
#log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
#log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
#log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
#log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4、log4j 日志效果
2020-09-15 23:00:14,182 DEBUG [com.bocom.dao.mapper.LoginMapper.queryUserByLoginAndPassword] - ==> Preparing: SELECT Id,UserName,Password,Gender,LoginName,Birthday,Image,ImageUrl FROM user WHERE LoginName=? AND Password=?
2020-09-15 23:00:14,250 DEBUG [com.bocom.dao.mapper.LoginMapper.queryUserByLoginAndPassword] - ==> Parameters: xiaomao(String), xiaomao123(String)
2020-09-15 23:00:14,301 DEBUG [com.bocom.dao.mapper.LoginMapper.queryUserByLoginAndPassword] - <== Total: 1