Java Log4j2 为什么要在 log4j 上使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30019585/
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
Log4j2 why would you use it over log4j?
提问by Scott Neville
I must be missing something but I have been looking at this for a few days now, but why on earth would you ever use log4j2 over log4j (other than the performance)?
我一定遗漏了一些东西,但我已经研究了几天了,但是你到底为什么要使用 log4j2 而不是 log4j(性能除外)?
From what I have seen so far, log4j2 is advertised as simpler to configure, but its actually vastly more complicated (been three days now and I still cant get it to write a log in my home directory). The auto configuration simply does not work for me (or at least I cant get it to work), the configuration file itself is substantially more complex in its structure and appears to be much harder to add things in at runtime to help diagnose.
从我目前所见,log4j2 被宣传为更易于配置,但实际上它要复杂得多(现在已经三天了,我仍然无法让它在我的主目录中写入日志)。自动配置根本不适合我(或者至少我不能让它工作),配置文件本身的结构要复杂得多,而且在运行时添加东西来帮助诊断似乎要困难得多。
So other than the performance is there any reason to use log4j2 over original log4j?
那么除了性能之外,还有什么理由在原始 log4j 上使用 log4j2 吗?
采纳答案by Remko Popma
Reasons to upgrade from Log4j 1.x to Log4j 2
从 Log4j 1.x 升级到 Log4j 2 的原因
Update: since August 2015, Log4j 1.x is officially End of Lifeand it is recommended to upgrade to Log4j 2. Update 2: Log4j 1.2 is broken in Java 9.
更新:自 2015 年 8 月起,Log4j 1.x 正式终止,建议升级到 Log4j 2。更新 2:Log4j 1.2 在 Java 9 中已损坏。
- Community support: Log4j 1.x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed.
- Async Loggers- performance similar to logging switched off
- Custom log levels
- Automatically reload its configuration upon modification without losing log events while reconfiguring.
- Java 8-style lambda supportfor lazy logging
- Log4j 2 is garbage-free(or at least low-garbage) since version 2.6
- Filtering: filtering based on context data, markers, regular expressions, and other components in the Log event. Filters can be associated with Loggers. You can use a common Filter class in any of these circumstances.
- Plugin Architecture- easy to extend by building custom components
- Supported APIs: SLF4J, Commons Logging, Log4j-1.x and java.util.logging
- Log4j 2 API separate from the Log4j 2 implementation. API supports more than just logging Strings: CharSequences, Objects and custom Messages. Messages allow support for interesting and complex constructs to be passed through the logging system and be efficiently manipulated. Users are free to create their own Message types and write custom Layouts, Filters and Lookups to manipulate them.
- Concurrency improvements: log4j2 uses java.util.concurrent libraries to perform locking at the lowest level possible. Log4j-1.x has known deadlock issues.
- Configuration via XML, JSON, YAML, properties configuration files or programmatically.
- 社区支持:Log4j 1.x 没有积极维护,而 Log4j 2 有一个活跃的社区,可以回答问题、添加功能并修复错误。
- 异步记录器-类似于关闭日志记录的性能
- 自定义日志级别
- 在修改时自动重新加载其配置,而不会在重新配置时丢失日志事件。
- 对延迟日志记录的Java 8 风格的lambda 支持
- 自 2.6 版以来,Log4j 2 是无垃圾的(或至少是低垃圾的)
- 过滤:根据日志事件中的上下文数据、标记、正则表达式和其他组件进行过滤。过滤器可以与记录器相关联。您可以在任何这些情况下使用公共 Filter 类。
- 插件架构- 通过构建自定义组件轻松扩展
- 支持的 API:SLF4J、Commons Logging、Log4j-1.x 和 java.util.logging
- Log4j 2 API 与 Log4j 2 实现分开。API 不仅支持记录字符串:CharSequences、Objects 和自定义Messages。消息允许通过日志系统传递有趣和复杂的结构并进行有效操作。用户可以自由创建自己的消息类型并编写自定义布局、过滤器和查找来操作它们。
- 并发改进:log4j2 使用 java.util.concurrent 库在可能的最低级别执行锁定。Log4j-1.x 存在已知的死锁问题。
- 通过 XML、JSON、YAML、属性配置文件或以编程方式进行配置。
Be aware
意识到
- log4j2.xml and log4j2.properties formats are different from the Log4j 1.2 configuration syntax
- Log4j 2 is not fullycompatible with Log4j 1.x: The Log4j 1.2 API is supported by the
log4j-1.2-api
adapter but customizations that rely on Log4j 1.2 internals may not work. - Java 6 required for version 2.0 to 2.3. Java 7 is required for Log4j 2.4 and later.
- log4j2.xml 和 log4j2.properties 格式与 Log4j 1.2 配置语法不同
- Log4j 2与 Log4j 1.x不完全兼容:
log4j-1.2-api
适配器支持 Log4j 1.2 API,但依赖于 Log4j 1.2 内部的自定义可能不起作用。 - 2.0 到 2.3 版本需要 Java 6。Log4j 2.4 及更高版本需要 Java 7。
Tips when upgrading
升级时的提示
Common issues people are having when getting started with log4j2:
人们在开始使用 log4j2 时遇到的常见问题:
- You need (at least) both log4j-api-2.6.2.jar and log4j-core-2.6.2.jar in your classpath
- Log4j2 looks for a log4j2.xml config file, not a log4j.xml config file
- Config file location: either put it in the classpath or specify its path with the
log4j.configurationFile
system property - To debug the configuration, use
<Configuration status="trace">
in the beginning of your config file - I would recommend starting with one of the many sample configurations provided in the log4j2 manual, then add more bells and whistles bit by bit.
- 您需要(至少)在类路径中同时使用 log4j-api-2.6.2.jar 和 log4j-core-2.6.2.jar
- Log4j2 查找 log4j 2.xml 配置文件,而不是 log4j.xml 配置文件
- 配置文件位置:将其放在类路径中或使用
log4j.configurationFile
系统属性指定其路径 - 要调试配置,请
<Configuration status="trace">
在配置文件的开头使用 - 我建议从 log4j2 手册中提供的众多示例配置之一开始,然后一点一点地添加更多的花里胡哨。
If your problem is not one of the above, please show your config and provide more detail on what problem you are experiencing. (Not sure what you expect from auto-configuration, this is a very basic function that logs ERROR events to the console if log4j2 cannot find a configuration file. This will rarely be sufficient.)
如果您的问题不是上述问题之一,请显示您的配置并提供有关您遇到的问题的更多详细信息。(不确定您对自动配置的期望,这是一个非常基本的功能,如果 log4j2 找不到配置文件,它会将 ERROR 事件记录到控制台。这几乎是不够的。)
To write to your home directory, you can use the system property lookup${sys:PROPERTYNAME}
. Below is an example configuration to demonstrate:
要写入您的主目录,您可以使用系统属性查找${sys:PROPERTYNAME}
。下面是一个示例配置来演示:
<Configuration status="trace">
<Properties>
<Property name="logfile">${sys:user.home}/log${date:yyyyMMdd}.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
<File name="FILE" fileName="${sys:logfile}">
<PatternLayout>
<pattern>%d %p [%t] %c{1.} %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="STDOUT" level="ERROR" />
<AppenderRef ref="FILE" />
</Root>
</Loggers>
</Configuration>
回答by JonasCz - Reinstate Monica
Check this. In short, from the link:
检查这个。简而言之,从链接:
Log4j 2.0 introduces:
a new plugin system
support for properties
support for JSON-based configuration and automatic reloading of its configuration.
Support for many existing logging frameworks, including SLF4J, Commons Logging, Apache Flume and Log4j 1.x, and provides a new programmer's API.
Log4j 2.0 引入:
一个新的插件系统
属性支持
支持基于 JSON 的配置并自动重新加载其配置。
支持许多现有的日志框架,包括 SLF4J、Commons Logging、Apache Flume 和 Log4j 1.x,并提供了新的程序员 API。
As you said it's also much faster.
正如你所说,它也快得多。
Disadvantages are:
缺点是:
log4j 2.0 is very different than log4j 1.x, and the API is mostly incompatible.
Difficult to set up.
log4j 2.0 与 log4j 1.x 非常不同,API 大多不兼容。
很难设置。
If you don't need any of the new features, you're probably fine with the older Log4j 1.x.
如果您不需要任何新功能,那么旧的 Log4j 1.x 可能没问题。