Log4j2为什么要在log4j上使用它? [英] Log4j2 why would you use it over log4j?

查看:121
本文介绍了Log4j2为什么要在log4j上使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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被宣传为配置更简单,但它实际上要复杂得多(现在已经三天了,而且我仍然无法在我的主目录中写日志)。自动配置对我来说根本不起作用(或者至少我不能使它工作),配置文件本身的结构要复杂得多,并且在运行时添加内容以帮助诊断似乎更难。

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而不是原始的log4j?

So other than the performance is there any reason to use log4j2 over original log4j?

推荐答案

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


  • 社区支持:Log4j 1.x没有主动维护,而Log4j 2有一个活跃的社区,可以回答问题,添加功能并修复错误。更新:自2015年8月起, Log4j 1.x正式停产,建议升级至Log4j 2 。更新2: Log4j 1.2在Java 9中被破坏

  • 异步记录器 - 性能类似于关闭日志

  • 自定义日志级别

  • 自动重新加载其配置在修改时不会在重新配置时丢失日志事件。

  • Java 8风格的 lambda support 用于延迟记录

  • Log4j 2是无垃圾<自版本2.6以来(或至少是低垃圾)

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

  • 插件架构 - 通过构建自定义组件轻松扩展

  • 支持的API:SLF4J,Commons Logging,Log4j-1.x和java.util.logging

  • Log4j 2 API与Log4j 2实现分开。 API支持的不仅仅是记录字符串:CharSequences,Objects和自定义消息。消息允许支持有趣和复杂的构造通过日志系统传递并被有效地操作。用户可以自由创建自己的消息类型并编写自定义布局,过滤器和查找来操作它们。

  • 并发改进:log4j2使用java.util.concurrent库在最低级别执行锁定可能。 Log4j-1.x已知死锁问题。

  • 通过XML,JSON,YAML,属性配置文件或以编程方式配置。

  • 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.
  • 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
  • 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.

注意


  • log4j2.xml和log4j2.properties格式与Log4j 1.2配置语法不同

  • Log4j 2不是完全与Log4j 1.x兼容: log4j-1.2-api 适配器,但依赖于Log4j 1.2内部的自定义可能无法正常工作。

  • 版本2.0到2.3需要Java 6。 Log4j 2.4及更高版本需要Java 7。

  • log4j2.xml and log4j2.properties formats are different from the Log4j 1.2 configuration syntax
  • 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.
  • Java 6 required for version 2.0 to 2.3. Java 7 is required for Log4j 2.4 and later.

升级时的提示

常见问题人们开始使用log4j2时:

Common issues people are having when getting started with log4j2:


  • 你需要(至少)类路径中的log4j-api-2.6.2.jar和log4j-core-2.6.2.jar

  • Log4j2查找log4j 2 .xml配置文件,而不是log4j.xml配置文件

  • 配置文件location :要么将其放在类路径中,要么使用 log4j.configurationFile指定其路径系统属性

  • 调试配置,在配置文件的开头使用< Configuration status =trace>

  • 我建议从log4j2手册中提供的众多示例配置之一,然后添加mo一点一点地响铃。

  • 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.

如果您的问题不是上述问题之一,请显示您的配置并提供更多详细信息你遇到了什么问题。 (不确定您对自动配置的期望,这是一个非常基本的功能,如果log4j2找不到配置文件,则会将ERROR事件记录到控制台。这很少就足够了。)

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

要写入您的主目录,您可以使用系统属性查找 $ {SYS:PROPERTYNAME} 。以下是演示的示例配置:

To write to your home directory, you can use the system property lookup ${sys:PROPERTYNAME}. Below is an example configuration to demonstrate:

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

这篇关于Log4j2为什么要在log4j上使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆