SLF4J介绍
SLF4J是Simple Logging Facade for Java 的简写,读法就是一个字母一个字母的读(官网的介绍视频就是这么读的)。SLF4J是一个接口,规定了一些记录日志的操作,有很多框架实现了该接口,比如:
- java.util.logging
- logback
- log4j
SLF4J需要配合这些实现框架才能使用
SLF4J使用
pom文件中引入slf4j-api
并编写如下代码测试:
public class App
{
private static final Logger logger = LoggerFactory.getLogger(App.class);
public static void main( String[] args )
{
logger.info("log test");
System.out.println( "Hello World!" );
}
}
结果如下:
Connected to the target VM, address: '127.0.0.1:54370', transport: 'socket'
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hello World!
提示没有绑定logger实现类,正如前文所说,SLF4J只是一个接口,并没有具体实现,它的实现依赖于各个框架,如果要正确使用,还需要引入相应的实现框架,下面介绍几种开发中常用的实现了SLF4J接口的框架
1. slf4j-simple
slf4j-simple是比较简单的SFL4J接口的实现,只需引入slf4j-api,不需要配置即可使用
pom文件:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
运行结果:
Connected to the target VM, address: '127.0.0.1:54905', transport: 'socket'
[main] INFO zhengxl.App - test log
Hello World!
2. logback
logback的ch.qos.logback.classic.Logger
类直接实现了SLF4J的org.slf4j.Logger
接口,所以他俩可以无缝搭配,建议采用该方式。
最简单的使用方式就是让logback使用默认配置,在没有logback.xml配置文件的情况下,logback会默认调用 BasicConfigurator
初始化一个最小配置。
pom文件:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>2
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
运行结果:
Connected to the target VM, address: '127.0.0.1:63141', transport: 'socket'
17:37:56.561 [main] INFO zhengxl.App - log test
Hello World!
3. log4j
要在SFL4J框架下使用log4j,需要用到slf4j-log4j12桥接起来。
pom:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
这里还需要增加log4j的配置文件:
log4j.properties
log4j.rootLogger=TRACE, STDOUT, ERRLOG
log4j.appender.encoding=UTF-8
#log to console
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Threshold=DEBUG
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=[%d][%-5p] (%F:%L) - %m%n
#log to file
log4j.appender.ERRLOG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERRLOG.Threshold=TRACE
log4j.appender.ERRLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.ERRLOG.layout.ConversionPattern=[%d][%-5p] (%F:%L) - %m%n
log4j.appender.ERRLOG.file=logs/error.log
log4j.appender.ERRLOG.DatePattern=-yyyyMMdd
运行结果:
[2020-02-12 23:48:53,454][INFO ] (App.java:17) - log test
Hello World!
4. slf4j-nop
该框架可以静默丢弃一切日志
pom:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.30</version>
</dependency>
运行结果:
Connected to the target VM, address: '127.0.0.1:65372', transport: 'socket'
Hello World!
SLF4J 最佳实践
在java语言中,由于String是不可变的,所以用 +
拼接字符效率非常低,SFL4J考虑到了这个问题,提供了占位符的方式来记录日志,最佳实践如下:
String name = "zhengxl5566";
Integer age = 18;
logger.info("hello!,{},{}",name,age);
输出:
[2020-02-12 23:53:12,078][INFO ] (App.java:20) - hello!,zhengxl5566,18