了解 Logback 和 Slf4j

Understanding Logback and Slf4j

我正在努力了解所有不同的日志记录工具(log4j、slf4j、logback、jcl 等)以及它们的作用。

我知道 slf4j 是不同日志记录工具的外观,可以很容易地在任何日志记录工具之间切换。但是当我谈到 logback 的话题时,我感到困惑。我知道 logback 是 log4j 的后继者,从 this post 开始,它使用 "natively implements";那到底是什么意思。据我了解,logback 与 slf4j 是一回事......所以它也是一个外观吗?我得到了关于 logback 是后端日志记录工具和与 slf4j 一样的面孔的混合描述。

我尝试了一个小测试项目来了解它是如何工作的。所以在我的 Maven pom 中我输入:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>

然后在我输入的代码中:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Foo {
    /** The Constant LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
}

那么这个实现是不是意味着slf4j是logback的门面,logback是和log4j、jcl一样的后端日志记录工具?

意味着SLF4J的接口中定义的所有方法都直接在Logback的class中实现。 "translate" SLF4J 调用实际日志记录实现没有附加层。

当您查看 org.slf4j.Logger 接口和实现它的 ch.qos.logback.classic.Logger class 时,可以很容易地看出这一点。它像手套一样合身。

简短的回答是 Logback 不是门面,而是类似于(但优于)log4j、java.util.logging、jcl 等的后端实现。继续阅读下面的详细答案:

下面的解释为@Kamayan 在他的回答中解释的内容添加了更多细节。逐条分解您的问题陈述。

I understand that slf4j is a facade to the different logging tools making it easy to switch between any logging tools.

简单来说,这意味着您 始终创建记录器并调用 SLF4J API 的记录方法,而不用担心实际实现 APIs 像 log4j 或 logback 等。当日志记录的行为由日志记录实现决定时,实际的实现只在运行时出现。要用一个简单的代码片段来映射它:

private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
LOGGER.debug("A debug log");

这里的Logger是org.slf4j.Logger,LoggerFactory是org.slf4j.LoggerFactory.

I understand logback is a successor to log4j and from this post it uses the words "natively implements"; what does that mean exactly.

Logger class in logback-classic implements all the methods that are present in Logger class SLF4J API(log4j、jcl 等不是这种情况)这意味着您使用 logback-classic 作为底层实现调用 SLF4J 记录器时产生零开销。

From what I'm understanding is that logback is the same thing as slf4j.....so is it a facade as well?

不,它不是外观。 更好的说法是它比同类产品更接近 slf4j 外观 - log4J、jcl 等

这是来自 logback 项目的 very nice article,它比较了 logback 和其他日志记录实现。