slf4j是Java里面用来记录日志的工具。本身并不实现任何记录日志的功能,里面只是接口或者抽象类。
The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time
在开始使用前,SLF4J 提供了一个用户手册,写的很详细,SLF4J Manual 大家可以查阅。下面的内容也是对手册的一些节选和编辑。
1、如果想使用slf4j,首先需要在项目的lib下添加slf4j-api-1.7.33.jar ,这是目前(2022年1月19日)slf4j最稳定的版本。文件里面提供了记录日志所需的所有方法。这个文件的下载地址如下:https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.33/slf4j-api-1.7.33.jar
2、接下来就要考虑你当前所使用的具体日志工具是什么,e.g. java.util.logging, logback, log4j,根据不同的工具,slf4j提供了不同的binding文件。
slf4j-log4j12-1.7.33.jar
Binding/provider for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path. Assuming you wish to continue to use the log4j 1.x framework, given that the log4j 1.x has reached EOL status, we strongly encourage you to use slf4j-reload4j instead. See below.
slf4j-reload4j-1.7.33.jar
SINCE 1.7.33 Binding/provider for reload4j framework. Reload4j is a drop-in replacement for log4j version 1.2.7. You also need to place reload4j.jar on your class path.
slf4j-jdk14-1.7.33.jar
Binding/provider for java.util.logging, also referred to as JDK 1.4 logging
slf4j-nop-1.7.33.jar
Binding/provider for NOP, silently discarding all logging.
slf4j-simple-1.7.33.jar
Binding/provider for Simple implementation, which outputs all events to System.err. Only messages of level INFO and higher are printed. This binding may be useful in the context of small applications.
slf4j-jcl-1.7.33.jar
Binding/provider for Jakarta Commons Logging. This binding will delegate all SLF4J logging to JCL.
logback-classic-1.2.10.jar (requires logback-core-1.2.10.jar)
NATIVE IMPLEMENTATION There are also SLF4J bindings/providers external to the SLF4J project, e.g. logback which implements SLF4J natively. Logback's ch.qos.logback.classic.Logger class is a direct implementation of SLF4J's org.slf4j.Logger interface. Thus, using SLF4J in conjunction with logback involves strictly zero memory and computational overhead.
下面以reload4j为例(什么是reload4j,请看这里:reload4j,reload4j其实就是log4j的一个解决了安全漏洞的版本,用法和log4j一样。)在使用reload4j的情况下,需要如引用的上文所述,下载slf4j-reload4j-1.7.33.jar 这个文件的下载地址:https://repo1.maven.org/maven2/org/slf4j/slf4j-reload4j/1.7.33/slf4j-reload4j-1.7.33.jar
下载后,如第一步所作,将文件放到lib下,并添加为类库(Add as Library)
******************************************************************************************
请注意,这里reload4j是对log4j 1.x版本的修改,目前log4j已经到了2.x的版本,slf4j的官网对这个log4j 2.x没有提供binding文件,如果要使用log4j 2.x的版本,可以参看如下:SLF4J Binding Using Log4j – Log4j 2 SLF4J Binding
这里写的很清楚,照着做就行了。
******************************************************************************************
3、完成上面两步以后,其实你已经完成了slf4j的使用前提。剩下的就是部署你需要的日志记录工具。这里用的是reload4j,到maven上下载了源代码,自己编译后生成 reload4j.jar ,再次将reload4j.jar 添加到 lib中,就可以使用了。
4、配置文件,这里的配置文件不是slf4j的配置文件,而是你真正使用的日志工具的配置文件,reload4j的配置文件和log4j一样,简单记录如下,如果想了解更多的配置文件的内容请参看:
log4j.properties 详解与配置步骤_sinat_30185177的博客-CSDN博客_log4j.properties
### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.err 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 ### 输出DEBUG 级别以上的日志到=D://log1/error.log ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = D://log1/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出ERROR 级别以上的日志到=D://log1/error.log ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =D://log1/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n