简体   繁体   English

Log4j、commons-logging、JDK-Logging 和 SLF4J 是如何相互关联的?

[英]How do Log4j, commons-logging, JDK-Logging and SLF4J relate to each other?

Are they alternatives, dependencies, APIs or implementations of each other?它们是替代品、依赖关系、API 还是彼此的实现? And why do they exist?它们为什么存在?

Ah, logging frameworks in Java.啊,Java 中的日志框架。 Your question mixes 2 different types of libraries:您的问题混合了 2 种不同类型的库:

  • log4j and JDK logging are libraries for handling logging log4j 和 JDK 日志记录是用于处理日志记录的库
  • Commons Logging and SLF4J are logging facades: you still need a real logging implementation (like log4j) Commons Logging 和 SLF4J 是日志门面:你仍然需要一个真正的日志实现(比如 log4j)

If you are writing a library that will be used in someone else's system, then you should use a logging facade because you do not know which logging framework they will use.如果您正在编写一个将在其他人的系统中使用的库,那么您应该使用日志外观,因为您不知道他们将使用哪个日志框架。 In this case use SLF4J (Commons Logging is older and has some classloader issues).在这种情况下,请使用 SLF4J(Commons Logging 较旧并且存在一些类加载器问题)。

If you control the whole application and can dictate which logging framework to use, you are free to choose your own preference.如果您控制整个应用程序并且可以指定使用哪个日志记录框架,您可以自由选择自己的偏好。 My preferred solutions are (in order of preference):我的首选解决方案是(按优先顺序):

  • Logback回溯
  • log4j log4j
  • JDK logging (in my opinion, a case of 'not invented here' by SUN) JDK 日志记录(在我看来,SUN 的“不是在这里发明的”案例)

I've been looking into this recently too.我最近也在研究这个。 I've been using Log4J for years with Commons Logging and recently switched to SLF4J.多年来,我一直在使用 Log4J 和 Commons Logging,最近切换到 SLF4J。

Log4j Log4j

Log4j is a framework for actually doing the log writing/distribution. Log4j 是一个实际进行日志写入/分发的框架。 It's extremely flexible: you can direct it to send log messages to files, syslog, remote monitoring, etc. You can also configure multiple loggers, logging categories, include context in entries, and so on.它非常灵活:您可以指示它将日志消息发送到文件、系统日志、远程监控等。您还可以配置多个记录器、记录类别、在条目中包含上下文等。 It's one of the most popular logging systems.它是最流行的日志记录系统之一。

JDK Logging JDK 日志记录

The built-in JDK logging (which I've never used, to be honest) was added in JDK 1.4.2. JDK 1.4.2 中添加了内置的 JDK 日志记录(老实说,我从未使用过)。 From what I gather, it's not very popular because it's not as flexible as Log4j, but I'd welcome comments:).据我所知,它不是很受欢迎,因为它不如 Log4j 灵活,但我欢迎评论:)。

Commons Logging and SLF4j Commons 日志记录SLF4j

Both of these are façades on top of various logging frameworks that present a common interface for your application.这两者都是各种日志框架之上的外观,为您的应用程序提供了一个通用接口。 For example, you can use CL/SLF4J in your application, and they will automatically detect an underlying logger implementation (Log4J, JDK logging, or a built-in logger that just delegates to System.err.println() ).例如,您可以在应用程序中使用 CL/SLF4J,它们将自动检测底层记录器实现(Log4J、JDK 日志记录或仅委托给System.err.println()的内置记录器)。 The benefit is that you or your end user can decide to switch out the underlying logging implementation at will, and they greatly simplify your implementation by removing many of the complexities of Log4J and JDK logging.好处是您或您的最终用户可以决定随意切换底层日志记录实现,它们通过消除 Log4J 和 JDK 日志记录的许多复杂性大大简化了您的实现。

Most often you will see them layered.大多数情况下,您会看到它们分层。

SLF4J is purely an abstraction layer and is not, in itself, used for the actual outputting of logging but used by you in your code to log messages. SLF4J 纯粹是一个抽象层,它本身并不用于实际的日志输出,而是由您在代码中用来记录消息。

A typical setup is to use SLF4J to log in your code, then use log4j as the underlying "output" layer using an appropriate slf4j->log4j bridge (a jar you just include on your classpath).一个典型的设置是使用 SLF4J 登录你的代码,然后使用 log4j 作为底层的“输出”层,使用适当的 slf4j->log4j 桥(一个 jar 你只包括在你的类路径中)。 In order to merge logging from different sources, various bridges exist.为了合并来自不同来源的日志记录,存在各种桥梁。 For instance, many app servers (like tomcat) will use JDK-logging to avoid forcing a "non standard" logging framework on the deployed applications.例如,许多应用服务器(如 tomcat)将使用 JDK 日志记录来避免在已部署的应用程序上强制使用“非标准”日志记录框架。 For that purpose, slf4j has a bridge that will pick up all output from JDK-logging.为此,slf4j 有一个桥接器,它将从 JDK 日志记录中获取所有 output。 So, this could be a stack所以,这可能是一个堆栈

JDK-logging <- Your app-server or framworks might log using this
  |
(JDK->Slf4j bridge)
  |
Slf4j <- your application logs using Slf4j
  |
(Slf4j->log4j bridge)
  |
log4j <- log4j is just responsible for outputting to the appenders you configure (file, console etc)

SLF4J just is a generic API with different back-ends (any other logging system). SLF4J 只是具有不同后端(任何其他日志记录系统)的通用 API。 Log4j and commons-logging (CL) different logging libraries, CL is a fossil. Log4j 和 commons-logging (CL) 不同的测井库,CL 是化石。 But they all have a fatal flaw , so sun have invented JDK logging.但是它们都有一个致命的缺陷,所以 sun 发明了 JDK 日志记录。

As for me, I prefer SLF4J as most flexible and logback as a backend for it.至于我,我更喜欢 SLF4J 作为最灵活的和logback作为它的后端。 Logback is most modern and have a lot nice features. Logback 是最现代的,并且有很多不错的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM