且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

新项目是否应使用logback而不是log4j?

更新时间:2023-09-13 09:48:40

您应该使用SLF4J + Logback进行记录。

You should use SLF4J+Logback for logging.

它提供了一些简洁的功能,如参数化消息和(与公共日志记录相比)映射诊断上下文(MDC, javadoc 文档)。

It provides neat features like parametrized messages and (in contrast to commons-logging) a Mapped Diagnostic Context (MDC, javadoc, documentation).

使用SLF4J使得日志记录后端可以非常优雅的方式进行交换。

Using SLF4J makes the logging backend exchangeable in a quite elegant way.

此外,SLF4J 支持将其他日志记录框架桥接到您将使用的实际SLF4J实现,以便第三方软件的日志记录事件将显示在统一日志中 - 除了java.util.logging无法以与其他日志框架相同的方式进行桥接。

Additionally, SLF4J supports bridging of other logging frameworks to the actual SLF4J implementation you'll be using so logging events from third party software will show up in your unified logs - with the exception of java.util.logging that can't be bridged the same way that other logging frameworks are.

javadocs。

Bridging jul is explained in the javadocs of SLF4JBridgeHandler.

我有一个在几个项目中使用SLF4J + Logback组合的非常好的经验和LOG4J开发几乎停滞不前。

I've had a very good experience using the SLF4J+Logback combination in several projects and LOG4J development has pretty much stalled.

SLF4J还有以下缺点:

SLF4J has the following remaining downsides:


  • 它不支持varargs与Java保持兼容< 1.5

  • 它不支持同时使用参数化消息和异常。

  • 它不包含对嵌套诊断上下文的支持( LOGCJ拥有的NDC, javadoc 。 / li>
  • It does not support varargs to stay compatible with Java < 1.5
  • It does not support using both parametrized message and an exception at the same time.
  • It does not contain support for a Nested Diagnostic Context (NDC, javadoc) which LOG4J has.