Log4j2为什么要在log4j上使用它?

时间:2022-07-25 21:49:17

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)?

我一定错过了一些东西,但我现在已经看了好几天,但为什么你会在log4j上使用log4j2(性能除外)?

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?

所以除了性能之外还有什么理由使用log4j2而不是原始的log4j?

2 个解决方案

#1


Reasons to upgrade from Log4j 1.x to Log4j 2

从Log4j 1.x升级到Log4j 2的原因

  • 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. Update: since August 2015, Log4j 1.x is officially End of Life and it is recommended to upgrade to Log4j 2. Update 2: Log4j 1.2 is broken in Java 9.
  • 社区支持:Log4j 1.x未得到主动维护,而Log4j 2有一个活跃的社区,可以回答问题,添加功能并修复错误。更新:自2015年8月起,Log4j 1.x正式停产,建议升级到Log4j 2.更新2:Log4j 1.2在Java 9中被破坏。

  • 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 support for lazy logging
  • Java 8风格的lambda支持延迟日志记录

  • Log4j 2 is garbage-free (or at least low-garbage) since version 2.6
  • 从版本2.6开始,Log4j 2是无垃圾的(或者至少是低垃圾)

  • 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.
  • 过滤:基于Log事件中的上下文数据,标记,正则表达式和其他组件进行过滤。过滤器可以与记录器关联。在任何这些情况下,您都可以使用通用的Filter类。

  • Plugin Architecture - easy to extend by building custom components
  • 插件架构 - 通过构建自定义组件轻松扩展

  • Supported APIs: SLF4J, Commons Logging, Log4j-1.x and java.util.logging
  • 支持的API:SLF4J,Commons Logging,Log4j-1.x和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.
  • Log4j 2 API与Log4j 2实现分开。 API支持的不仅仅是记录字符串:CharSequences,Objects和自定义消息。消息允许支持有趣和复杂的构造通过日志系统传递并被有效地操作。用户可以*创建自己的消息类型,并编写自定义布局,过滤器和查找来操作它们。

  • Concurrency improvements: log4j2 uses java.util.concurrent libraries to perform locking at the lowest level possible. Log4j-1.x has known deadlock issues.
  • 并发性改进:log4j2使用java.util.concurrent库在尽可能最低的级别执行锁定。 Log4j-1.x已知死锁问题。

  • Configuration via XML, JSON, YAML, properties configuration files or programmatically.
  • 通过XML,JSON,YAML,属性配置文件或以编程方式配置。

Be aware

  • log4j2.xml and log4j2.properties formats are different from the Log4j 1.2 configuration syntax
  • log4j2.xml和log4j2.properties格式与Log4j 1.2配置语法不同

  • Log4j 2 is not fully compatible 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.
  • Log4j 2与Log4j 1.x不完全兼容:log4j-1.2-api适配器支持Log4j 1.2 API,但依赖Log4j 1.2内部的自定义可能无效。

  • Java 6 required for version 2.0 to 2.3. Java 7 is required for Log4j 2.4 and later.
  • 版本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
  • 你需要(至少)你的类路径中的log4j-api-2.6.2.jar和log4j-core-2.6.2.jar

  • Log4j2 looks for a log4j2.xml config file, not a log4j.xml config file
  • Log4j2查找log4j2.xml配置文件,而不是log4j.xml配置文件

  • Config file location: either put it in the classpath or specify its path with the log4j.configurationFile system property
  • 配置文件位置:将其放在类路径中,或使用log4j.configurationFile系统属性指定其路径

  • 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.
  • 我建议从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>

#2


Check this. In short, from the link:

检查一下。简而言之,从链接:

Log4j 2.0 introduces:

Log4j 2.0介绍:

  • a new plugin system

    一个新的插件系统

  • support for properties

    对财产的支持

  • support for JSON-based configuration and automatic reloading of its configuration.

    支持基于JSON的配置和自动重新加载其配置。

Support for many existing logging frameworks, including SLF4J, Commons Logging, Apache Flume and Log4j 1.x, and provides a new programmer’s API.

支持许多现有的日志记录框架,包括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.

    log4j 2.0与log4j 1.x非常不同,API大多不兼容。

  • Difficult to set up.

    很难设置。

If you don't need any of the new features, you're probably fine with the older Log4j 1.x.

如果您不需要任何新功能,那么使用较旧的Log4j 1.x可能会很好。

#1


Reasons to upgrade from Log4j 1.x to Log4j 2

从Log4j 1.x升级到Log4j 2的原因

  • 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. Update: since August 2015, Log4j 1.x is officially End of Life and it is recommended to upgrade to Log4j 2. Update 2: Log4j 1.2 is broken in Java 9.
  • 社区支持:Log4j 1.x未得到主动维护,而Log4j 2有一个活跃的社区,可以回答问题,添加功能并修复错误。更新:自2015年8月起,Log4j 1.x正式停产,建议升级到Log4j 2.更新2:Log4j 1.2在Java 9中被破坏。

  • 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 support for lazy logging
  • Java 8风格的lambda支持延迟日志记录

  • Log4j 2 is garbage-free (or at least low-garbage) since version 2.6
  • 从版本2.6开始,Log4j 2是无垃圾的(或者至少是低垃圾)

  • 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.
  • 过滤:基于Log事件中的上下文数据,标记,正则表达式和其他组件进行过滤。过滤器可以与记录器关联。在任何这些情况下,您都可以使用通用的Filter类。

  • Plugin Architecture - easy to extend by building custom components
  • 插件架构 - 通过构建自定义组件轻松扩展

  • Supported APIs: SLF4J, Commons Logging, Log4j-1.x and java.util.logging
  • 支持的API:SLF4J,Commons Logging,Log4j-1.x和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.
  • Log4j 2 API与Log4j 2实现分开。 API支持的不仅仅是记录字符串:CharSequences,Objects和自定义消息。消息允许支持有趣和复杂的构造通过日志系统传递并被有效地操作。用户可以*创建自己的消息类型,并编写自定义布局,过滤器和查找来操作它们。

  • Concurrency improvements: log4j2 uses java.util.concurrent libraries to perform locking at the lowest level possible. Log4j-1.x has known deadlock issues.
  • 并发性改进:log4j2使用java.util.concurrent库在尽可能最低的级别执行锁定。 Log4j-1.x已知死锁问题。

  • Configuration via XML, JSON, YAML, properties configuration files or programmatically.
  • 通过XML,JSON,YAML,属性配置文件或以编程方式配置。

Be aware

  • log4j2.xml and log4j2.properties formats are different from the Log4j 1.2 configuration syntax
  • log4j2.xml和log4j2.properties格式与Log4j 1.2配置语法不同

  • Log4j 2 is not fully compatible 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.
  • Log4j 2与Log4j 1.x不完全兼容:log4j-1.2-api适配器支持Log4j 1.2 API,但依赖Log4j 1.2内部的自定义可能无效。

  • Java 6 required for version 2.0 to 2.3. Java 7 is required for Log4j 2.4 and later.
  • 版本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
  • 你需要(至少)你的类路径中的log4j-api-2.6.2.jar和log4j-core-2.6.2.jar

  • Log4j2 looks for a log4j2.xml config file, not a log4j.xml config file
  • Log4j2查找log4j2.xml配置文件,而不是log4j.xml配置文件

  • Config file location: either put it in the classpath or specify its path with the log4j.configurationFile system property
  • 配置文件位置:将其放在类路径中,或使用log4j.configurationFile系统属性指定其路径

  • 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.
  • 我建议从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>

#2


Check this. In short, from the link:

检查一下。简而言之,从链接:

Log4j 2.0 introduces:

Log4j 2.0介绍:

  • a new plugin system

    一个新的插件系统

  • support for properties

    对财产的支持

  • support for JSON-based configuration and automatic reloading of its configuration.

    支持基于JSON的配置和自动重新加载其配置。

Support for many existing logging frameworks, including SLF4J, Commons Logging, Apache Flume and Log4j 1.x, and provides a new programmer’s API.

支持许多现有的日志记录框架,包括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.

    log4j 2.0与log4j 1.x非常不同,API大多不兼容。

  • Difficult to set up.

    很难设置。

If you don't need any of the new features, you're probably fine with the older Log4j 1.x.

如果您不需要任何新功能,那么使用较旧的Log4j 1.x可能会很好。