Slf4j 简明教程

SLF4J - Logging Frameworks

记录在编程中指的是记录活动/事件。通常,应用程序开发人员应该负责记录。

Logging in programming, refers to recording activities/events. Usually, the application developers should take care of logging.

为了让记录工作变得更容易,Java 提供了各种框架 − log4J、java.util.logging (JUL)、tiny log、logback 等。

To make the job of logging easier, Java provides various frameworks − log4J, java.util.logging (JUL), tiny log, logback, etc.

Logging Framework Overview

一个记录框架通常包含三个元素 −

A logging framework usually contains three elements −

Logger

捕获消息和元数据。

Captures the message along with the metadata.

Formatter

格式化记录器捕获的消息。

Formats the messages captured by the logger.

Handler

处理程序或附录器最终通过在控制台上打印、存储在数据库中或通过电子邮件发送来调度消息。

The Handler or appender finally dispatches the messages either by printing on the console or, by storing in the database or, by sending through an email.

一些框架将记录器和附录器元素结合在一起,以加快操作速度。

Some frameworks combine the logger and appender elements to speed up the operations.

Logger Object

为了记录消息,应用程序发送一个记录器对象(有时还包括异常,如果存在的话),其中包含名称和安全级别。

To log a message, the application sends a logger object (sometimes along with the exceptions if any) with name and security level.

Severity Level

记录的消息将具有不同的级别。下表列出了常规的记录级别。

The messages logged will be of various levels. The following table lists down the general levels of logging.

Sr.No

Severity level & Description

1

Fatal Severe issue that causes the application to terminate.

2

ERROR Runtime errors.

3

WARNING In most cases, the errors are due to the usage of deprecated APIs.

4

INFO Events that occur at runtime.

5

DEBUG Information about the flow of the system.

6

TRACE More detailed information about the flow of the system.