Java log4j 与 logback

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/178215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:56:16  来源:igfitidea点击:

log4j vs logback

javalogginglog4jlogback

提问by

We are using log4j behind a selfmade wrapper. We plan to use much more features of it now.

我们在自制包装器后面使用 log4j。我们现在计划使用它的更多功能。

Should we update to logback ?

我们应该更新到 logback 吗?

(I mean the framework not a facade like SLF4J)

(我的意思是框架不是像 SLF4J 这样的外观)

回答by toolkit

Not exactly answering your question, but if you could move away from your self-made wrapper then there is Simple Logging Facade for Java (SLF4J)which Hibernate has now switched to (instead of commons logging).

不能完全回答您的问题,但是如果您可以摆脱自制的包装器,那么Hibernate 现在已经切换到了Simple Logging Facade for Java (SLF4J)(而不是公共日志记录)。

SLF4J suffers from none of the class loader problems or memory leaks observed with Jakarta Commons Logging (JCL).

SLF4J 没有遇到 Jakarta Commons Logging (JCL) 中观察到的类加载器问题或内存泄漏。

SLF4J supports JDK logging, log4j and logback. So then it should be fairly easy to switch from log4j to logback when the time is right.

SLF4J 支持 JDK 日志记录、log4j 和 logback。那么在适当的时候从 log4j 切换到 logback 应该相当容易。

Edit: Aplogies that I hadn't made myself clear. I was suggesting using SLF4J to isolate yourself from having to make a hard choice between log4j or logback.

编辑:我没有说清楚的道歉。我建议使用 SLF4J 来隔离自己,不必在 log4j 或 logback 之间做出艰难的选择。

回答by Carl Smotricz

Your decision should be based on

你的决定应该基于

  • your actual need for these "more features"; and
  • your expected cost of implementing the change.
  • 您对这些“更多功能”的实际需求;和
  • 您实施变更的预期成本。

You should resist the urge to change APIs just because it's "newer, shinier, better." I follow a policy of "if it's not broken, don't kick it."

你应该抵制改变 API 的冲动,因为它“更新、更闪亮、更好”。我遵循“如果它没有坏掉,就不要踢它”的政策。

If your application requires a very sophisticated logging framework, you may want to consider why.

如果您的应用程序需要非常复杂的日志记录框架,您可能需要考虑原因。

回答by Ceki

Logback natively implements the SLF4J API. This means that if you are using logback, you are actually using the SLF4J API. You could theoretically use the internals of the logback API directly for logging, but that is highly discouraged. All logback documentation and examples on loggers are written in terms of the SLF4J API.

Logback 原生实现了 SLF4J API。这意味着,如果您使用的是 logback,那么您实际上是在使用 SLF4J API。理论上,您可以直接使用 logback API 的内部结构进行日志记录,但强烈建议不要这样做。所有关于记录器的 logback 文档和示例都是根据 SLF4J API 编写的。

So by using logback, you'd be actually using SLF4J and if for any reason you wanted to switch back to log4j, you could do so within minutes by simply dropping slf4j-log4j12.jar onto your class path.

因此,通过使用 logback,您实际上是在使用 SLF4J,如果出于任何原因您想切换回 log4j,只需将 slf4j-log4j12.jar 放到您的类路径中,您就可以在几分钟内完成。

When migrating from logback to log4j, logback specific parts, specifically those contained in logback.xmlconfiguration file would still need to be migrated to its log4j equivalent, i.e. log4j.properties. When migrating in the other direction, log4j configuration, i.e. log4j.properties, would need to be converted to its logback equivalent. There is an on-line toolfor that. The amount of work involved in migrating configuration files is muchless than the work required to migrate logger calls disseminated throughout all your software's source code and its dependencies.

当从 logback 迁移到 log4j 时,特定于 logback 的部分,特别是那些包含在logback.xml配置文件中的部分,仍然需要迁移到它的 log4j 等价物,即log4j.properties。当向另一个方向迁移时,log4j 配置,即log4j.properties,需要转换为它的 logback 等价物。有一个在线工具可以做到这一点。迁移配置文件所涉及的工作量少于迁移在所有软件源代码及其依赖项中传播的记录器调用所需的工作量。

回答by AWhitford

Should you? Yes.

你应该? 是的

Why? Log4Jhas essentially been deprecated by Logback.

为什么? Log4J基本上已被Logback弃用。

Is it urgent? Maybe not.

紧急吗?也许不吧。

Is it painless? Probably, but it may depend on your logging statements.

是无痛的吗?可能,但这可能取决于您的日志记录。

Note that if you really want to take full advantage of LogBack (or SLF4J), then you really need to write proper logging statements. This will yield advantages like faster code because of the lazy evaluation, and less lines of code because you can avoid guards.

请注意,如果您真的想充分利用 LogBack(或 SLF4J),那么您确实需要编写适当的日志记录语句。由于惰性求值,这将产生诸如更快的代码和更少的代码行等优点,因为您可以避免保护。

Finally, I highly recommend SLF4J. (Why recreate the wheel with your own facade?)

最后,我强烈推荐 SLF4J。(为什么用自己的外观重新创建轮子?)

回答by Dima

Mature project or even project deep into development stages would probably loose more than gain from such upgrade, IMHO. Logback is certainly much more advanced in an array of points, but not to an extent for complete replacement in a working system. I would certainly consider logback for a new development, but existing log4j is good enough and mature for anything already released and met end user. This is very subjective, you should see cost yourself.

恕我直言,成熟的项目甚至深入开发阶段的项目可能会从这种升级中获得更多收益。Logback 在一系列点上肯定要先进得多,但还没有达到在工作系统中完全替换的程度。我当然会考虑将 logback 用于新的开发,但是现有的 log4j 对于任何已经发布并遇到最终用户的东西来说已经足够好和成熟了。这是非常主观的,你应该自己看看成本。

回答by Christian

In the logging world there are Facades (like Apache Commons Logging, slf4j or even the Log4j 2.0 API) and implementations (Log4j 1 + 2, java.util.logging, TinyLog, Logback).

在日志世界中,有 Facades(如 Apache Commons Logging、slf4j 甚至 Log4j 2.0 API)和实现(Log4j 1 + 2、java.util.logging、TinyLog、Logback)。

Basically you should replace your selfmade wrapper with slf4j IF and only IF you are not happy with it for some reason. While Apache Commons Logging is not really providing a modern API, slf4j and the new Log4j 2 facade is providing that. Given that quite a bunch of apps use slf4j as a wrapper it might make sense to use that.

基本上你应该用 slf4j IF 替换你自制的包装器,并且只有当你出于某种原因对它不满意时。虽然 Apache Commons Logging 并没有真正提供现代 API,但 slf4j 和新的 Log4j 2 外观正在提供。鉴于相当多的应用程序使用 slf4j 作为包装器,使用它可能是有意义的。

slf4j gives a number of nice API sugar, like this example from slf4j docs:

slf4j 提供了许多不错的 API 糖,就像 slf4j 文档中的这个例子:

logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);

logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);

It's variable substitution. This is also supported by Log4j 2.

这是变量替换。Log4j 2 也支持这一点。

However you need to be aware that slf4j is developed by QOS who also maintain logback. Log4j 2.0 is baked in the Apache Software Foundation. In the past three years a vibrant and active community has grown there again. If you appreciate Open Source as it is done by the Apache Software Foundation with all its guarantees you might reconsider using slf4j in favor to use Log4j 2 directly.

但是你需要知道 slf4j 是由 QOS 开发的,他们也维护 logback。Log4j 2.0 在 Apache 软件基金会中诞生。在过去的三年里,一个充满活力和活跃的社区在那里再次成长。如果您喜欢开源,因为它由 Apache 软件基金会完成并提供所有保证,您可能会重新考虑使用 slf4j,转而直接使用 Log4j 2。

Please note:

请注意:

In the past log4j 1 was not actively maintained while Logback was. But today things are different. Log4j 2 is actively maintained and releases on almost regular schedule. It also includes lot of modern features and -imho- makes a couple of things better than Logback. This is sometimes just a matter of taste and you should draw your own conclusions.

在过去,log4j 1 没有被积极维护,而 Logback 是。但今天情况有所不同。Log4j 2 得到积极维护并几乎定期发布。它还包括许多现代功能,恕我直言,它比 Logback 做了一些更好的事情。有时这只是品味问题,您应该得出自己的结论。

I wrote a quick overview on the new Features of Log4j 2.0: http://www.grobmeier.de/the-new-log4j-2-0-05122012.html

我写了一个关于 Log4j 2.0 新特性的快速概述:http: //www.grobmeier.de/the-new-log4j-2-0-05122012.html

When reading you will see that Log4j 2 was inspired by Logback but also by other logging frameworks. But the code base is different; it shares almost nothing with Log4j 1 and zero with Logback. This lead to some improvements like in example Log4j 2 operates with bytestreams instead of Strings under the hood. Also it doesn't loose events while reconfiguring.

在阅读时,您会看到 Log4j 2 的灵感来自 Logback,但也受到其他日志框架的启发。但是代码库是不同的;它与 Log4j 1 几乎不共享任何内容,与 Logback 共享零。这导致了一些改进,例如 Log4j 2 在后台使用字节流而不是字符串进行操作。此外,它在重新配置时不会丢失事件。

Log4j 2 can log with higher speed than other frameworks I know: http://www.grobmeier.de/log4j-2-performance-close-to-insane-20072013.html

Log4j 2 可以比我知道的其他框架以更高的速度登录:http: //www.grobmeier.de/log4j-2-performance-close-to-insane-20072013.html

And still the user community seems to be much bigger than Logbacks: http://www.grobmeier.de/apache-log4j-is-the-leading-logging-framework-06082013.html

而且用户社区似乎比 Logbacks 大得多:http: //www.grobmeier.de/apache-log4j-is-the-leading-logging-framework-06082013.html

That all said the best idea is you choose the logging frameworks which just fits best to what you want to achieve. I would not switch a full framework if I would disable logging in production environment and just perform basic logging in my app. However if you do a bit more with logging just look at the features which are provided by the frameworks and their developers. While you get commercial support for Logback through QOS (i heard) there is currently no commercial support for Log4j 2. On the other hand if you need to do audit logging and need high performance provided by the async appenders it makes a lot of sense to check log4j 2.

总而言之,最好的想法是您选择最适合您想要实现的目标的日志记录框架。如果我在生产环境中禁用日志记录并仅在我的应用程序中执行基本日志记录,我不会切换完整框架。但是,如果您对日志记录进行更多操作,只需查看框架及其开发人员提供的功能即可。虽然您通过 QOS 获得了对 Logback 的商业支持(我听说),但目前没有对 Log4j 2 的商业支持。另一方面,如果您需要进行审计日志记录并需要异步附加程序提供的高性能,这很有意义检查 log4j 2。

Please note despite all comfort they provide, facades always eat a little performance. It's maybe not affecting you at all, but if you are on low resources you may need to save everything you can have.

请注意,尽管它们提供了所有的舒适度,但外墙总是会消耗一些性能。它可能根本不会影响您,但是如果您的资源不足,您可能需要保存您可以拥有的一切。

Without knowing you requirements better it is almost impossible to give a recommendation. Just: don't switch just because a lot of people switch. Switch only because you see value of it. And the argumentation that log4j is dead doesn't count anymore. It's alive, and it's hot.

如果不更好地了解您的需求,则几乎不可能给出建议。只是:不要因为很多人切换而切换。切换只是因为您看到了它的价值。并且 log4j 已死的争论不再重要。它活着,而且很热。

DISCLAIMER: I am currently VP, Apache Logging Services and involved in log4j as well.

免责声明:我目前是 Apache 日志服务副总裁,也参与了 log4j。