Java Log4j 会被 Slf4j 抛弃吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2063883/
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
Is Log4j being abandoned in favor of Slf4j?
提问by ruchirhhi
It seems that log4j has some class loading issues(among others) and it seems to me the trend is to move out of log4j toward slf4j. (Hibernate stopped using the first in favor of the latter)
log4j 似乎有一些类加载问题(等等),在我看来,趋势是从 log4j 转向 slf4j。(Hibernate 停止使用第一个,转而使用后者)
- Is it true?
- What are the main issues in log4j that slf4j solves?
- Is slf4j the final word or is there even a better "the next next log4j" industry standard?
- 这是真的吗?
- slf4j 解决的 log4j 的主要问题是什么?
- slf4j 是最终决定还是有更好的“下一个 log4j”行业标准?
Update:
更新:
- So this answerby delfuego confuses me, can you accept / object it?:
- 所以delfuego的这个答案让我感到困惑,你能接受/反对吗?:
You appear to have stumbled upon the major problem with log4j (and the Apache Commons Logging library), namely that they have a ridiculously hard time discovering and interacting with the right classloaders as they're being used. There's a very dense explanation, complete with examples, here; the take-home message is that one of the primary driving forces for the new logging framework SLF4J was to eliminate these issues entirely.You might want to swap it in and see if your life is made any easier.
您似乎偶然发现了 log4j(和 Apache Commons Logging 库)的主要问题,即它们在使用时发现正确的类加载器并与之交互非常困难。这里有一个非常详细的解释,并附有示例;重要的信息是,新日志框架 SLF4J 的主要驱动力之一是完全消除这些问题。您可能想将其换入,看看您的生活是否变得更轻松。
- More classloading issues by log4j: http://articles.qos.ch/classloader.html
- log4j 的更多类加载问题:http: //articles.qos.ch/classloader.html
采纳答案by BalusC
Slf4j is indeed just a logging facade. However, Log4j is intended to be succeeded by Logback, from the very same authors.
Slf4j 确实只是一个日志外观。但是,Log4j 旨在由来自相同作者的Logback接替。
Update: if you'd like to know about another benefit of Slf4j, it's the fact that following (ugly) constructs aren't needed anymore to avoid the toString()
unnecessarily been called:
更新:如果您想了解 Slf4j 的另一个好处,事实是不再需要以下(丑陋的)构造以避免toString()
不必要的调用:
if (logger.isDebugEnabled()) {
logger.debug("Message: " + bigObject + ", " + anotherBigObject);
}
You can instead make use of parameterized messages:
您可以改为使用参数化消息:
logger.debug("Message: {}, {}", bigObject, anotherBigObject);
Also see What is the fastest way of (not) logging?
另请参阅什么是(非)日志记录的最快方式?
回答by Jon Skeet
Looking at the slf4j pageit doesn't look like it would replacelog4j - it would just allow you to use the same underlying logging framework (e.g. log4j) for your whole application, allowing libraries to hook into that automatically.
查看slf4j 页面,它看起来不会取代log4j - 它只会允许您对整个应用程序使用相同的底层日志记录框架(例如 log4j),从而允许库自动挂钩。
It looks more like a replacement for Apache Commons Loggingthan log4j.
它看起来更像是Apache Commons Logging的替代品,而不是 log4j。
回答by Thomas L?tzer
SLF4J has, in my opinion, the huge advantage that you can unify the logging of all the libraries that you use through the bridges that it provides. Neither of the other logging frameworks allows this. This allows projects to smoothly move to SLF4J and ignore the logging framework choices that dependencies have made.
在我看来,SLF4J 有一个巨大的优势,你可以通过它提供的桥来统一你使用的所有库的日志记录。其他日志框架都不允许这样做。这允许项目顺利转移到 SLF4J 并忽略依赖项所做的日志框架选择。
回答by EJB
Slf4J is not an alternative for Log4j, but rather provides a Fa?adefor logging, so one can you can plug in your own logging framework. It's mainly useful for libraries. from slf4j.org:
Slf4J 不是 Log4j 的替代品,而是提供了一个用于日志记录的Fa?ade,因此您可以插入自己的日志记录框架。它主要用于图书馆。来自 slf4j.org:
The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.
Simple Logging Facade for Java 或 (SLF4J) 作为各种日志框架的简单外观或抽象,例如 java.util.logging、log4j 和 logback,允许最终用户在部署时插入所需的日志框架。
To answer your question: Slf4j is being adopted by frameworks now, but in your projects, you can keep on using Log4J (or any other)
回答您的问题:Slf4j 现在正在被框架采用,但是在您的项目中,您可以继续使用 Log4J(或任何其他)
回答by Kartoch
First: an important point: Slf4j is the frontend logging (the API), which can use below most of the main loggin systems: log4j or java.util.logging for instance. So it is better to compared sfl4j to commons-logging.
第一:重要的一点:Slf4j 是前端日志(API),它可以在下面的大多数主要登录系统中使用:例如 log4j 或 java.util.logging。所以最好将 sfl4j 与commons-logging进行比较。
About the state of Log4j, quotes from The state of java logging(one year ago)
关于 Log4j 的状态,引自The state of java logging(一年前)
One thing that I hadn't realized is that log4j development is essentially dead. It's currently at version 1.2, and plans for version 1.3 were abandoned in favour of developing log4j 2.0. However, it doesn't appear that 2.0 is in active development. It is worth noting that Ceki Gülcü, the original founder of the log4j project, has moved on to slf4j (see below).
我没有意识到的一件事是 log4j 开发基本上已经死了。它目前是 1.2 版,1.3 版的计划被放弃,转而开发 log4j 2.0。但是,似乎 2.0 并未处于积极开发阶段。值得注意的是,log4j 项目的原始创始人 Ceki Gülcü 已经转向 slf4j(见下文)。
回答by Steven S Wang
Slf4j is not a real logging facade. Slf4j does not support many features of its implementors. For short, I mention log4j examples below.
Slf4j 不是真正的日志记录外观。Slf4j 不支持其实现者的许多功能。简而言之,我在下面提到了 log4j 示例。
- Slf4j cannot specify a user selected configuration file, but forces user to use default (log4j.properties or log4j.xml) at one of so many Java roots (each Jar has one root plus JVM root and classes or bin). If two JAR files have it, it is hard to control which one to use safely.
- Slf4j cannot support all Log4j levels, such as 'fatal'. When switch big code from Log4j to Slf4j, huge code change effort is needed (e.g. deciding how to rearrange levels).
- Two key Jar files (log4j-over-slf4j.jar or slf4j-log4j12.jar) must be chosen. If classpath both, won't work. If choose one randomly, lose unexpected features (e.g. log4j-over-slf4j.jar does not support multiple log files for same classes; e.g. one for events log and one for raw data log).
- Slf4j 无法指定用户选择的配置文件,而是强制用户在众多 Java 根之一(每个 Jar 有一个根加上 JVM 根和类或 bin)使用默认值(log4j.properties 或 log4j.xml)。如果有两个 JAR 文件,很难控制安全使用哪一个。
- Slf4j 无法支持所有 Log4j 级别,例如“fatal”。当将大代码从 Log4j 切换到 Slf4j 时,需要大量的代码更改工作(例如决定如何重新排列级别)。
- 必须选择两个关键 Jar 文件(log4j-over-slf4j.jar 或 slf4j-log4j12.jar)。如果 classpath 两个,都行不通。如果随机选择一个,会丢失意外的特性(例如 log4j-over-slf4j.jar 不支持同一类的多个日志文件;例如一个用于事件日志,一个用于原始数据日志)。