Slf4j 简明教程
SLF4J - Environment Setup
在本章中,我们将讲解如何在 Eclipse IDE 中设置 SLF4J 环境。在继续安装之前,请确保您的系统中已安装 Eclipse。如果没有,请下载并安装 Eclipse。
In this chapter, we will explain how to set SLF4J environment in Eclipse IDE. Before proceeding with the installation, make sure that you already have Eclipse installed in your system. If not, download and install Eclipse.
欲了解更多有关 Eclipse 的信息,请参阅我们的 Eclipse Tutorial
For more information on Eclipse, please refer our Eclipse Tutorial
Step 1: Download the dependency JAR file
打开 SLF4J 网站的官方 homepage ,并转到下载页面。
Open the official homepage of the SLF4J website and go to the download page.
现在,根据您的操作系统下载 slf4j-X.X.tar.gz 或 slf4j-X.X.zip 的最新稳定版本(如果为 Windows,则为 .zip 文件,如果为 Linux,则为 tar.gz 文件)。
Now, download the latest stable version of slf4j-X.X.tar.gz or slf4j-X.X.zip, according to your operating system (if windows .zip file or if Linux tar.gz file).
在下载的文件夹中,您将找到 slf4j-api-X.X.jar。这是必需的 Jar 文件。
Within the downloaded folder, you will find slf4j-api-X.X.jar. This is the required Jar file.
Step 2: Create a project and set build path
打开 Eclipse 并创建一个示例项目。右键单击该项目,选择选项 Build Path → Configure Build Path… ,如下所示。
Open eclipse and create a sample project. Right-click on the project, select the option Build Path → Configure Build Path… as shown below.
在 Libraries 标签中的 Java Build Path 框架中,点击 Add External JARs… 。
In the Java Build Path frame in the Libraries tab, click Add External JARs…
选择下载的 slf4j-api.x.x.jar 文件,并点击 Apply and Close 。
Select the slf4j-api.x.x.jar file downloaded and click Apply and Close.
SLF4J Bindings
除了 slf4j-api.x.x.jar 文件之外, SLF4J 还提供了一些其他 Jar 文件,如下所示。它们被称为 SLF4J bindings 。
In addition to slf4j-api.x.x.jar file, SLF4J provides several other Jar files as shown below. These are called SLF4J bindings.
其中每个绑定都针对其各自的日志记录框架。
Where each binding is for its respective logging framework.
下表列出了 SLF4J 绑定及其对应的框架。
The following table lists the SLF4J bindings and their corresponding frameworks.
Sr.No |
Jar file & Logging Framework |
1 |
slf4j-nop-x.x.jar No operation, discards all loggings. |
2 |
slf4j-simple-x.x.jar Simple implementation where messages for info and higher are printed and, remaining all outputs to System.err. |
3 |
slf4j-jcl-x.x.jar Jakarta Commons Logging framework. |
4 |
slf4j-jdk14-x.x.jar Java.util.logging framework (JUL). |
5 |
slf4j-log4j12-x.x.jar Log4J frame work. In addition, you need to have log4j.jar. |
要让 SLF4J 与 slf4l-api-x.x.jar 一起工作,你需要在该项目(设置构建路径)的类路径中添加所需的记录器框架的相应 Jar 文件(绑定)。
To make SLF4J work along with slf4l-api-x.x.jar, you need to add the respective Jar file (binding) of the desired logger framework in the classpath of the project (set build path).
要从一个框架切换到另一个框架,你需要替换相应的绑定。如果未找到绑定,则它会默认为无操作模式。
To switch from one framework to other, you need to replace the respective binding. If no bounding is found, it defaults to no-operation mode.
Pom.xml for SLF4J
如果你正在创建 maven 项目,请打开 pom.xml 并在其中粘贴以下内容,然后刷新项目。
If you are creating the maven project, open the pom.xml and paste the following content in it and refresh the project.
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Sample</groupId>
<artifactId>Sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>