intro to Apache Log4j 2

来源:互联网 发布:郑州网络维修 编辑:程序博客网 时间:2024/05/22 12:19

【0】README

0.1)本文作为 原文(http://logging.apache.org/log4j/2.x/)的译文,仅作参考, 旨在了解 Log4j 2 的相关知识
0.2) Apache Log4j 2 是Log4j的升级版,并对其前任Log4j 1.x 提供了很多改进和对 LogBack架构中的固有问题进行了修复。
0.3) 文末转载了原文(http://logging.apache.org/log4j/2.x/);


【1】一些Log4j 2 的特点和改进方面如下:

1.1)API分离:

  • Log4j 的API 从 实现中分离出来,并使得应用程序员对于使用什么样的类和方法更加清晰,但他们确定要保证先前兼容性的话。该改进允许Log4j 团队以兼容方式安全地改善其实现。

1.2)提高性能:

  • Log4j 2 包括了下一代基于LMAX分裂库的异步日志记录器。在多线程环境中, 相比于Log4j1.x ,异步日志记录器有高于其18倍的吞吐量和更低数量级的延迟。Logback 详情参见: https://logging.apache.org/log4j/2.x/manual/async.html#Performance 。其他的,在一些关键领域上,Log4j 2比 Log4j 1.x 执行地更快速,且Log4j 2 与大多数情况下的 Logback 架构有相似的地方,详情参见:https://logging.apache.org/log4j/2.x/performance.html 。

1.3)支持多种API:

  • 鉴于 Log4j 2 API提供了最佳性能,Log4j 2 也提供了对 SLF4J 和 通用Loging API 的支持;

1.4)自动重载配置信息:

  • 如 Logback 架构, 只要有更新,Log4j 2 能够自动重载其配置信息。与Logback 不同的是, Log4j 2这样做并没有遗漏日志事件;

1.5)高级过滤功能:

  • 如Logback架构,Log4j 2支持基于上下文数据、标记,正则表达式和其它日志事件中组件的过滤。在所有的事件被传递给日志记录器前或者当他们通过目的地传输类(Appender)时,可以对它们进行过滤。此外,过滤器还可以和日志记录器关联起来。和Logback 不同的是,你可以在任何情况下使用一个常见的过滤器。

1.6)插件架构:

  • Log4j 使用插件模式配置组件。同样地,你也不需要写代码来创建和配置目的地传输类(Appender),布局类(Layout),模式转换器类(Pattern Converter) 等等。Log4j 自动识别和使用组件,当有配置信息引用它们的时候。

1.7)属性支持:

  • 你可以引用配置信息里的属性,Log4j 将直接替换它们, 或者Log4j 将它们传递给底层组件,这些组件将动态地处理它们。属性值来源于定义在配置文件,系统属性,环境变量,线程上下文映射(ThreadContect Map)以及日志事件中数据的值。用户可以增加他们自己的 Lookup(http://logging.apache.org/log4j/2.x/manual/lookups.html) 插件来进一步自定义属性提供器;

1.8)Java 8 Lambda(λ) 支持:

  • 在以前,如果构造一条日志消息成本很高的话,那么在构建该日志消息前,你经常会很明确地检查是否该请求日志级别可用(enabled)。运行在 Java 8 上面的客户端代码可得益于Log4j 的 Lambda 支持。因为Log4j 不会去评估一个lambda(λ)表达式,如果该请求日志级别不可用的话, 即是说,更少的代码产生了相同的结果;

1.9)定制的日志级别:

  • 在Log4j 2中, 定制的日志级别很容易地在代码和配置信息中定义。不需要进行子类化。

1.10)文档:

  • Log4j 2的用户只能在本网址(http://logging.apache.org/log4j/2.x/manual/index.html)可以获得,也可以查看可下载的PDF文件(http://logging.apache.org/log4j/2.x/log4j-users-guide.pdf)

1.11)要求

  • Log4j 2.4 以及以上版本需要 Java 7 ,或者 java 6 2.0-alpha 1 到2.3。一些功能特点需要可选依赖性, 而文档指定了Log4j 2 功能特点所需要的依赖性;

1.12)新闻:

  • Log4j 2 现在可以用于生产。Log4j 2 的API和 Log4j 1.x 不兼容, 但 适配器允许应用继续使用 Log4j 1.x 的API 。适配器对于Apache 常见的日志记录类(Apache Commons Logging) 和 SLF4J 还是可用的。

Apache Log4j 2
Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.

Some of the features and improvements in Log4j 2 are:

API Separation
The API for Log4j is separate from the implementation making it clear for application developers which classes and methods they can use while ensuring forward compatibility. This allows the Log4j team to improve the implementation safely and in a compatible manner.

Improved Performance
Log4j 2 contains next-generation Asynchronous Loggers based on the LMAX Disruptor library. In multi-threaded scenarios Asynchronous Loggers have 18 times higher throughput and orders of magnitude lower latency than Log4j 1.x and Logback. See Asynchronous Logging Performance for details. Otherwise, Log4j 2 performs faster than Log4j 1.x in critical areas and similarly to Logback under most circumstances. See Performance for more information.

Support for multiple APIs
While the Log4j 2 API will provide the best performance, Log4j 2 provides support for the SLF4J and Commons Logging APIs.

Automatic Reloading of Configurations
Like Logback, Log4j 2 can automatically reload its configuration upon modification. Unlike Logback, it will do so without losing log events while reconfiguration is taking place.

Advanced Filtering
Like Logback, Log4j 2 supports filtering based on context data, markers, regular expressions, and other components in the Log event. Filtering can be specified to apply to all events before being passed to Loggers or as they pass through Appenders. In addition, filters can also be associated with Loggers. Unlike Logback, you can use a common Filter class in any of these circumstances.

Plugin Architecture
Log4j uses the plugin pattern to configure components. As such, you do not need to write code to create and configure an Appender, Layout, Pattern Converter, and so on. Log4j automatically recognizes plugins and uses them when a configuration references them.

Property Support
You can reference properties in a configuration, Log4j will directly replace them, or Log4j will pass them to an underlying component that will dynamically resolve them. Properties come from values defined in the configuration file, system properties, environment variables, the ThreadContext Map, and data present in the event. Users can further customize the property providers by adding their own Lookup Plugin.

Java 8 Lambda Support
Previously, if a log message was expensive to construct, you would often explicitly check if the requested log level is enabled before constructing the message. Client code running on Java 8 can benefit from Log4j’s lambda support. Since Log4j will not evaluate a lambda expression if the requested log level is not enabled, the same effect can be achieved with less code.
Custom Log Levels
In Log4j 2, custom log levels can easily be defined in code or in configuration. No subclassing is required.

Documentation
The Log4j 2 User’s Guide is available on this site or as a downloadable PDF.

Requirements
Log4j 2.4 and greater requires Java 7, versions 2.0-alpha1 to 2.3 required Java 6. Some features require optional dependencies; the documentation for these features specifies the dependencies.

News
Log4j 2 is now available for production. The API for Log4j 2 is not compatible with Log4j 1.x, however an adapter is available to allow applications to continue to use the Log4j 1.x API. Adapters are also available for Apache Commons Logging and SLF4J.

0 0
原创粉丝点击