Slf4j 简明教程

SLF4J - Hello world

在本章中,我们将看到一个使用 SLF4J 的简单基本记录程序。按照以下描述的步骤编写一个简单的记录程序。

In this chapter, we will see a simple basic logger program using SLF4J. Follow the steps described below to write a simple logger.

Step 1 - Create an object of the slf4j.Logger interface

因为 slf4j.Logger 是 SLF4J API 的入口点,您首先需要获取/创建它的对象。

Since the slf4j.Logger is the entry point of the SLF4J API, first, you need to get/create its object

LoggerFactory 类的 getLogger() 方法接受一个表示名称的字符串值,并返回一个带有指定名称的 Logger 对象。

The getLogger() method of the LoggerFactory class accepts a string value representing a name and returns a Logger object with the specified name.

Logger logger = LoggerFactory.getLogger("SampleLogger");

Step 2 - Log the required message

slf4j.Logger 接口的 info() 方法接受一个表示所需消息的字符串值,并以信息级别记录它。

The info() method of the slf4j.Logger interface accepts a string value representing the required message and logs it at the info level.

logger.info("Hi This is my first SLF4J program");

Example

以下是演示如何使用 SLF4J 在 Java 中编写示例记录程序的程序。

Following is the program that demonstrates how to write a sample logger in Java using SLF4J.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4JExample {
   public static void main(String[] args) {
      //Creating the Logger object
      Logger logger = LoggerFactory.getLogger("SampleLogger");

      //Logging the information
      logger.info("Hi This is my first SLF4J program");
   }
}

Output

在最初运行以下程序时,您将获得以下输出,而不是所需的消息。

On running the following program initially, you will get the following output instead of the desired message.

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.

正如本教程前面提到的,由于我们尚未将类路径设置为表示日志框架的任何绑定,SLF4J 默认实现无操作。因此,若要查看消息,您需要在项目类路径中添加所需绑定。由于我们使用的是 Eclipse,请针对各自的 JAR 文件设置 build path ,或在 pom.xml 文件中添加其依赖项。

Since we have not set the classpath to any binding representing a logging framework, as mentioned earlier in this tutorial, SLF4J defaulted to a no-operation implementation. So, to see the message you need to add the desired binding in the project classpath. Since we are using eclipse, set build path for respective JAR file or, add its dependency in the pom.xml file.

例如,如果我们需要使用 JUL(Java.util.logging 框架),则需要为 JAR 文件 slf4j-jdk14-x.x.jar 设置构建路径。如果我们要使用 log4J 日志框架,则需要为 JAR 文件 slf4j-log4j12-x.x.jarlog4j.jar 设置构建路径,或添加依赖项。

For example, if we need to use JUL (Java.util.logging framework), we need to set build path for the jar file slf4j-jdk14-x.x.jar. And if we want to use log4J logging framework, we need to set build path or, add dependencies for the jar files slf4j-log4j12-x.x.jar and log4j.jar.

将表示任何日志框架(除 slf4j-nopx.x.jar 以外)的绑定添加到项目(类路径)之后,您将获得以下输出。

After adding the binding representing any of the logging frameworks except slf4j-nopx.x.jar to the project (classpath), you will get the following output.

Dec 06, 2018 5:29:44 PM SLF4JExample main
INFO: Hi Welcome to Tutorialspoint