简体   繁体   English

比较log4j和Logger

[英]Compare log4j and Logger

How does The JDK's Logger compare to Apache log4j ? JDK的Logger与Apache log4j相比如何?

Which one is better for new projects that target Java 6? 针对Java 6的新项目哪个更好? How do they compare in terms of flexibility and configurability? 他们如何在灵活性和可配置性方面进行比较?

To my mind the only thing the JDK Logger has going for it is that it is part of the JDK, so it doesn't add an external dependency. 在我看来,JDK Logger唯一能做的就是它是JDK的一部分,所以它不会添加外部依赖。 If the choice is only between those two, I'd go with Log4j. 如果选择只在这两者之间,我会选择Log4j。 It still has better support in terms of appenders, the number of people who know it (in my anecdotal observations), and a better API (that is subjective as well). 对于appender,知道它的人数(在我的轶事观察中)以及更好的API(也是主观的),它仍然有更好的支持。

Starting a project today, the most tempting thing to do is go with slf4j and deffer the decision - you can always plug in a different framework underneath slf4j by just changing the classpath. 今天开始一个项目,最吸引人的事情就是使用slf4j并做出决定 - 你可以通过更改类路径随时插入slf4j下面的不同框架。

That being said there are other options (such as Log5j) that take advantage of the latest Java language features. 话虽如此,还有其他选项(如Log5j)可以利用最新的Java语言功能。 I'd recommend taking a long look Logback (from one of the main programmers of Log4j, as is slf4j). 我建议长期看看Logback (来自Log4j的主要程序员之一,和slf4j一样)。

I've never used the direct JDK logger, but for a new project I'd highly reccomend Logback , designed as a successor for log4j. 我从来没有使用直接JDK记录器,但是对于一个新项目,我非常推荐Logback ,它被设计为log4j的后继产品。 Some of the very nice things you can do with it: 你可以用它做的一些非常好的事情:

  • printf style parameter building, no more messy concatenating strings protected by if logger.isDebugEnabled() guards. printf样式参数构建,如果logger.isDebugEnabled()保护,则不会受到更多混乱的串联字符串的保护。

go from 从...来

if (log.isDebugEnabled())
 { 
    log.warn (i + "many ints,"+ l+"many longs");
}

to

log.debug("{} many ints, {} many longs", i, l);
  • very flexible config, including configurations that will print traces. 非常灵活的配置,包括将打印跟踪的配置。 The config is xml, but their site includes a utility that will generate an xml config from your log4j config to get you started. 配置是xml,但是它们的站点包含一个实用程序,它将从log4j配置生成xml配置以帮助您入门。

Downside - lots of packages require log4j anyway, since it's so common, so your project may need to include 2 logging packages. 缺点 - 许多软件包无论如何都需要log4j,因为它很常见,因此您的项目可能需要包含2个日志包。

For a new project I would strongly recommend the slf4j project which provides generic frontend to several logger frameworks. 对于一个新项目,我强烈推荐slf4j项目,该项目为几个记录器框架提供通用前端。

The {}-syntax described by Steve B is present in slf4j too (same author) so you get all the benefits regardless of backend, and STILL get the backend independency. Steve B描述的{} -syntax也存在于slf4j中(同一作者),因此无论后端如何都能获得所有好处,并且STILL获得后端独立性。 Also a log4j bridge is available so existing code transparently can use slf4j. 此外,还有一个log4j桥,因此现有代码可以透明地使用slf4j。 It is really nice. 真的很好。

For actual backend, logback is nice but you may already have invested in log4j - this is easily leveragable. 对于实际的后端,logback很不错,但您可能已经投入了log4j - 这很容易实现。

I would recommend the JDK logging API . 我推荐JDK日志API I have used it for many years, without any problem whatsoever. 我已经使用了很多年,没有任何问题。 It's part of the JDK, so no extra jar is required. 它是JDK的一部分,因此不需要额外的jar。 The distinctions between log4j and JDK logging are small, and, in my opinion, don't justify the use of log4j. log4j和JDK日志记录之间的区别很小,在我看来,不能证明log4j的使用是正确的。

Standard API vs log4j 标准API与log4j

A common question asked by Java developers is: Java开发人员提出的一个常见问题是:

Why we should use log4j logging framework when Java provides an API for logging. 当Java提供用于记录的API时,为什么我们应该使用log4j日志框架。 The java API can be accessed by (java.util.logging)? java API可以通过(java.util.logging)访问?

Log4j has following advantages over standard logging API: 与标准日志API相比,Log4j具有以下优势:

  • log4j provides robust logging log4j提供强大的日志记录
  • log4j has more features available than standard logging API log4j比标准日志API具有更多可用功能
  • configuring and using log4j is easier 配置和使用log4j更容易
  • log4j also has a much more robust formatting system log4j还有一个更强大的格式化系统
  • many add-on programs and handlers are available for log4j log4j可以使用许多附加程序和处理程序

I struggled with this question several times; 我好几次都在努力解决这个问题; with jdk7 and jdk8; 用jdk7和jdk8; java logging super fast and slick, but only thing we need to put programmer hat while configuring. java logging super fast and slick,但是我们只需要配置程序员帽子。

To log to multiple file destiantions, we neeed to create signare classes extending FielHandler and use them in properties file to configure, this kind intricacies appears time consuming but once you get hang efficiency and reliability are awesome. 要记录多个文件destiantions,我们需要创建扩展FielHandler的signare类并在属性文件中使用它们进行配置,这种复杂性看起来很耗时但是一旦你获得挂起效率和可靠性就很棒了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM