Scott Neville Scott Neville - 7 months ago 70
Java Question

Log4j2 why would you use it over log4j?

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

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.

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

Answer

Reasons to upgrade from Log4j 1.x to 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.
  • Async Loggers - performance similar to logging switched off
  • Custom log levels
  • Automatically reload its configuration upon modification without losing log events while reconfiguring.
  • 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
  • Concurrency improvements: log4j2 uses java.util.concurrent libraries to perform locking at the lowest level possible. Log4j-1.x has known deadlock issues.

Be aware

  • Configuration via XML, JSON, or YAML: log4j.properties format is not supported
  • Log4j 2 is not fully compatible with Log4j 1.x
  • requires at least Java 6

Tips when upgrading

Common issues people are having when getting started with log4j2:

  • You need (at least) both log4j-api-2.2.jar and log4j-core-2.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.

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

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