slf4j+log4j的使用

时间:2021-10-06 14:29:05
  • maven依赖引入
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
  • log4j.properties 配置(根据实际情况追加详细配置) 放在resource目录下
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p] %d{MM-dd HH:mm:ss,SSS} [%c{1}] - %m%n
  • 单元测试
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class LoggerTest { private final static Logger logger = LoggerFactory.getLogger(LoggerTest.class); @Test
public void testLogOut(){
logger.info("日志输出:{} {}", "2017-01-01","00:00:00");
}
}

输出结果:

[INFO] 02-08 17:12:10,184 [LoggerTest] - 日志输出:2017-01-01 00:00:00