原生 Logback VS 通过 SLF4J 的 Logback
我阅读了以下有关 Java 可用日志框架的文章: http://michaelandrews.typepad.com/the_technical_times/2011/04 /java-logging-reconsidered.html
作者提到过将 SLF4J 与 Logback 结合使用。这与直接使用 Logback 有什么不同。如果直接使用 Logback 而不是使用 SLF4J 不是更好,因为 Logback 是构建在 SLF4J 之上的。
I have gone through the following article regarding the logging frameworks available for Java:
http://michaelandrews.typepad.com/the_technical_times/2011/04/java-logging-reconsidered.html
The author has mentioned using SLF4J with Logback. How is that different from using Logback directly. Wouldn't it be better if one uses Logback directly rather than going for SLF4J, since Logback is built on top of SLF4J.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SLF4J 为 Logback 添加了零开销,因为它只是 Logback 实现的接口,没有任何附加层。
您应该使用 SLF4J 只是因为...
直接访问 Logback 的唯一地方是在应用程序中手动(重新)配置日志记录时。有时会出现这种需要,但即使在这种情况下,使用 Logback 也将仅限于单个类甚至方法。
根据经验:库应始终使用日志记录抽象,而应用程序定义它们正在使用的日志记录,可以选择直接访问它。
SLF4J is adding zero overhead to Logback since it is simply the interface that is implemented by Logback without any additional layer.
You should use SLF4J simply because...
The only place where you'd access Logback directly would be while (re)configuring your logging manually in an application. The need for this arises occasionally but even in that case, working with Logback would be restricted to a single class or even method.
As a rule of thumb: libraries should always use a logging abstraction while applications define the logging they are using, optionally accessing it directly.
SLF4J 几乎不增加任何开销,并且 Logback 具有与其的本机绑定。
如果您 100% 知道将来不需要切换到其他日志框架,请使用 logback native。但 SLF4J 允许您进行一些抽象,并且您可以瞬间切换日志记录后端。
Logback 不是构建在 SLF4J 之上的。 SLF4J 是一个日志记录抽象框架。它本身不进行任何日志记录。它只是提供统一的日志记录接口。
SLF4J adds almost no overhead and Logback has a native bindings to it.
If you know by 100% that you will not need to switch to other logging framework in the future, go with logback native. But SLF4J allows you some abstraction and you can switch logging backends in a blink.
Logback is not build on top of SLF4J. SLF4J is an abstraction framework for logging. It doesn't do any logging itself. It just provides unified interface for logging.