当前位置: 首页 > 工具软件 > anylog > 使用案例 >

Log4j 2.8.2官方简介

司空均
2023-12-01

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.

Aache Log4j 2 是log4j的升级版,在其上个版本基础上有了巨大提高,远超过Log4J 1.x,在Logback基础上提供更多的提高,虽然一些Logback的架构本身的东西还是固定的


Features 特点


API Separation API 分离

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.

为了在保证兼容性的基础上使应用程序开发者清楚他们使用的类和方,将Log4j的api分离。这允许log4j团队去改进提高安全性和兼容性



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 significantly outperforms
Log4j 1.x, Logback and java.util.logging, especially in multi-threaded
applications. See Performance for more information.

Log4j 2 在LMAX破坏库基础上,包含下一代异步日志。在多线程异步日志方面吞吐量达到18+次,并且比起log4j 1.x在一定程度上降低了潜在问题(重新登录)出现,查看异步日志性能 细节。其他方面,Logback和java.util.logging ,log4j 2 均显著超过 log4j 1.x,特别是在多线程程序中 ,查看性能更多信息。



Support for multiple APIs 对多重API的支持

While the Log4j 2 API will provide the best performance, Log4j 2
provides support for the Log4j 1.2, SLF4J, Commons Logging and
java.util.logging (JUL) APIs.

Log4j 2 将提供最好的性能同时,log4j 2 提供对 Log4j 1.2, SLF4J, Commons Logging and java.util.logging (JUL) APIs.的支持



Avoid lock-in 避免死锁

Applications coded to the Log4j 2 API always have the option to use
any SLF4J-compliant library as their logger implementation with the
log4j-to-slf4j adapter.

应用程序代码为log4j 2 API 总是,需要选择使用任何SLF4J-compliant库,作为那些使用log4j-to-slf4j适配器实现的日志



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.

像Logback一样,log4j 2 能够根据修改自动重新装载他的配置,不同于Logback,log4j在重新装载配置是,将不会丢失日志事件。



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.

和logback一样,log4j 2 在上下文数据,标记,正则表达式,及其他日志事件组件基础上支持过滤器。过滤器可以被指定在将要被传到日志或者他们通过输出源之前申请所有事件。除此之外,过滤器同样也可以和日志关联起来。不同于logback,你能使用一个公共过滤器类在这些情况下。



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.

Log4j使用插件模式去组装组件。正是这样,你不需要写代码去创建,并且配置一个输出源,布局,模式转换等等。Log4j 自动识别插件并且使用他们,当一个配置引用他们
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.

你可以引用功能在一个配置红,log4j 将直接引用他们,或者log4j将通过他们传递给一个底层的组件,将动态的处理他们。功能来自配置文件,系统功能,环境变量,线程内容映射,以及事件数据显示中定义的值。用户可以进一步定制,一些功能过呢通过添加他们自己的插件进行提供。



Java 8 Lambda Support 对java8 ‘拉姆达’ 的支持

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.

以前,如果一个日志构建开销太大,在构建消息之前,你需要经常明确的筛选请求日志标准是否被使用。客户代码运行在java8环境下能够得益于log4j2 对java8‘拉姆达’的支持,自从log4j 无法估计一个拉姆达表达式当请求日志标准将不被使用,同样影响能够被获取将减少代码量。



Custom Log Levels 定制日志标准

In Log4j 2, custom log levels can easily be defined in code or in
configuration. No subclassing is required.

在log4j 2 中 定制日志标准可以简单的通过代码中定义或者配置中定义,必须没有子类化



Garbage-free 没有垃圾

During steady state logging, Log4j 2 is garbage-free in stand-alone
applications, and low garbage in web applications. This reduces
pressure on the garbage collector and can give better response time
performance.

在稳定日志起见,在独立的应用程序中log4j 2 是没有垃圾,在web应用程序中有更少的垃圾。这减少了垃圾回收器的压力也使得相应在时间性能上更好



Documentation 文档

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

Log4j 2 的用户向导是可使用的在这个网址,或者作为下载的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.

Log4j 2.4 和较大的依赖java7,2.0版本-2.3 依赖java6.一些产品依赖是可选择的;文档中介绍这些产品指定的依赖



News 新闻

Log4j 2.8.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, SLF4J, and
java.util.logging.

Log4j 2.8.2 设计目前可用的产品,Log4j的API不能兼容log4j 1.x,然而可以使用一个适配器去允许应用程序继续使用log4j 1.x的API,适配器也可以用于Apache公告Loging,SLF4J, and java.util.logging。

Log4j 2.8.2 is the latest release of Log4j and contains several bug
fixes that were found after the release of Log4j 2.6. The list of
fixes can be found in the latest changes report.

Log4j 2.8.2是最新发布的log4j,并且包含log4j2.6发布之后被发现的服务漏洞的修复。这些修复能够在最新的改变报告中被找到。

Note that subsequent to the release of Log4j 2.6 a minor source
incompatibility with prior release was found due to the addition of
new methods to the Logger interface. If you have code that does:

注意,继Log4j 2.6发布之后,发现和之前版本有较小的资源不相容,由于在Logger接口添加新的方法,如果你已经编码需要做下面的事

logger.error(null, "This is the log message", throwable); 

or similar with any log level you will get a compiler error saying the reference is ambiguous. To correct this either do:

或者和任何日志标准相似的,你将会得到一个报错,参考出现歧义。更正任何一个

logger.error("This is the log message", throwable);

or

logger.error((Marker) null, "This is the log message", throwable);

Log4j 2.8.2 maintains binary compatibility with previous releases.

Log4j 2.8.2 会继续兼容之前版本

 类似资料: