intro to Apache Log4j 2
【0】README
0.1)本文作為 原文(http://logging.apache.org/log4j/2.x/)的譯文,僅作參考, 旨在了解 Log4j 2 的相關(guān)知識 ;
0.2) Apache Log4j 2 是Log4j的升級版,并對其前任Log4j 1.x 提供了很多改進(jìn)和對 LogBack架構(gòu)中的固有問題進(jìn)行了修復(fù)。
0.3) 文末轉(zhuǎn)載了原文(http://logging.apache.org/log4j/2.x/);
【1】一些Log4j 2 的特點(diǎn)和改進(jìn)方面如下:
1.1)API分離:
- Log4j 的API 從 實(shí)現(xiàn)中分離出來,并使得應(yīng)用程序員對于使用什么樣的類和方法更加清晰,但他們確定要保證先前兼容性的話。該改進(jìn)允許Log4j 團(tuán)隊(duì)以兼容方式安全地改善其實(shí)現(xiàn)。
1.2)提高性能:
- Log4j 2 包括了下一代基于LMAX分裂庫的異步日志記錄器。在多線程環(huán)境中, 相比于Log4j1.x ,異步日志記錄器有高于其18倍的吞吐量和更低數(shù)量級的延遲。Logback 詳情參見: https://logging.apache.org/log4j/2.x/manual/async.html#Performance 。其他的,在一些關(guān)鍵領(lǐng)域上,Log4j 2比 Log4j 1.x 執(zhí)行地更快速,且Log4j 2 與大多數(shù)情況下的 Logback 架構(gòu)有相似的地方,詳情參見:https://logging.apache.org/log4j/2.x/performance.html 。
1.3)支持多種API:
- 鑒于 Log4j 2 API提供了最佳性能,Log4j 2 也提供了對 SLF4J 和 通用Loging API 的支持;
1.4)自動(dòng)重載配置信息:
- 如 Logback 架構(gòu), 只要有更新,Log4j 2 能夠自動(dòng)重載其配置信息。與Logback 不同的是, Log4j 2這樣做并沒有遺漏日志事件;
1.5)高級過濾功能:
- 如Logback架構(gòu),Log4j 2支持基于上下文數(shù)據(jù)、標(biāo)記,正則表達(dá)式和其它日志事件中組件的過濾。在所有的事件被傳遞給日志記錄器前或者當(dāng)他們通過目的地傳輸類(Appender)時(shí),可以對它們進(jìn)行過濾。此外,過濾器還可以和日志記錄器關(guān)聯(lián)起來。和Logback 不同的是,你可以在任何情況下使用一個(gè)常見的過濾器。
1.6)插件架構(gòu):
- Log4j 使用插件模式配置組件。同樣地,你也不需要寫代碼來創(chuàng)建和配置目的地傳輸類(Appender),布局類(Layout),模式轉(zhuǎn)換器類(Pattern Converter) 等等。Log4j 自動(dòng)識別和使用組件,當(dāng)有配置信息引用它們的時(shí)候。
1.7)屬性支持:
- 你可以引用配置信息里的屬性,Log4j 將直接替換它們, 或者Log4j 將它們傳遞給底層組件,這些組件將動(dòng)態(tài)地處理它們。屬性值來源于定義在配置文件,系統(tǒng)屬性,環(huán)境變量,線程上下文映射(ThreadContect Map)以及日志事件中數(shù)據(jù)的值。用戶可以增加他們自己的 Lookup(http://logging.apache.org/log4j/2.x/manual/lookups.html) 插件來進(jìn)一步自定義屬性提供器;
1.8)Java 8 Lambda(λ) 支持:
- 在以前,如果構(gòu)造一條日志消息成本很高的話,那么在構(gòu)建該日志消息前,你經(jīng)常會(huì)很明確地檢查是否該請求日志級別可用(enabled)。運(yùn)行在 Java 8 上面的客戶端代碼可得益于Log4j 的 Lambda 支持。因?yàn)長og4j 不會(huì)去評估一個(gè)lambda(λ)表達(dá)式,如果該請求日志級別不可用的話, 即是說,更少的代碼產(chǎn)生了相同的結(jié)果;
1.9)定制的日志級別:
- 在Log4j 2中, 定制的日志級別很容易地在代碼和配置信息中定義。不需要進(jìn)行子類化。
1.10)文檔:
- Log4j 2的用戶只能在本網(wǎng)址(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。一些功能特點(diǎn)需要可選依賴性, 而文檔指定了Log4j 2 功能特點(diǎn)所需要的依賴性;
1.12)新聞:
- Log4j 2 現(xiàn)在可以用于生產(chǎn)。Log4j 2 的API和 Log4j 1.x 不兼容, 但 適配器允許應(yīng)用繼續(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.
總結(jié)
以上是生活随笔為你收集整理的intro to Apache Log4j 2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑cpu多核排行榜(intel双核cp
- 下一篇: java 调试技巧