是否有技术原因更喜欢使用 logback 而不是 log4j? [英] Are there technical reasons to prefer using logback instead of log4j?

查看:20
本文介绍了是否有技术原因更喜欢使用 logback 而不是 log4j?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新项目是否应该使用 logback 而不是 log4j 作为日志框架?

或者换句话说:'logback 是否比 log4j 更好(将 SLF4J-logback 的'特性'放在一边)?'

解决方案

您应该使用 SLF4J+Logback 进行日志记录.

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

使用 SLF4J 可以以一种非常优雅的方式交换日志后端.

此外,SLF4J 支持将其他日志框架桥接到您将使用的实际 SLF4J 实现来自第三方软件的日志记录事件将显示在您的统一日志中——java.util.logging 除外,它无法像其他日志记录框架一样进行桥接.

桥接 jul 在 SLF4JBridgeHandler 的 javadocs 中有解释.>

我在多个项目中使用 SLF4J+Logback 组合的体验非常好,但 LOG4J 开发几乎停滞不前.

SLF4J 还存在以下缺点:

  • 它不支持可变参数以与 Java 保持兼容
  • 1.5
  • 它不支持同时使用参数化消息和异常.
  • 它不包含对嵌套诊断上下文的支持(NDC,javadoc) LOG4J 有.

Should new projects use logback instead of log4j as a logging framework ?

Or with other words :'Is logback better than log4j (leaving the SLF4J-'feature' of logback beside)?'

解决方案

You should use SLF4J+Logback for logging.

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

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

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.

Bridging jul is explained in the javadocs of SLF4JBridgeHandler.

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

SLF4J has the following remaining downsides:

  • 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.

这篇关于是否有技术原因更喜欢使用 logback 而不是 log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆