配置及使用log4j
(1)mybatis核心配置文件导入依赖
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
(2)配置log4j.properties
### 设置###
#将等级为debug的日志文件输出到控制台console、stdout 文件file
log4j.rootLogger = DEBUG,stdout,file
### 输出信息到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
#文件输出相关设置
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File = ./log/lin.log
log4j.appender.file.Append = true
log4j.appender.file.MaxFileSize = 10mb
log4j.appender.file.Threshold = DEBUG
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
(3)mapper配置log4j为日志的实现
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
(4)使用
简单使用
1.在要使用log4j的类中导包
import org.apache.log4j.Logger;
2.日志对象,参数为当前类的class
static Logger logger=Logger.getLogger(UserMapperTest.class);
3.日志级别
@Test
public void testLog4j(){
logger.debug("debug:进入了testLog4j");
logger.error("error:进入了testLog4j");
logger.info("info:进入了testLog4j");
}