Apache Log4j 2 is an updated version of Log4j that offers significant performance improvements over Log4j 1.x, providing the functionality provided by Logback while addressing the inherent problems of the Logback architecture.

Review images

New features and enhancements

API

  • The separation of Log4j’s API and implementation has the advantage that application developers can clearly see which classes and methods are available and which are not for downward compatibility. This also allows the Log4j team to optimize concrete implementations without worrying about compatibility.

Performance improvement

  • Log4j 2 includes the next generation of asynchronous logging tools based on the LMAX Disruptor class library. The asynchronous logging tool has 18 times the throughput of Log4j 1.x and Logback in multi-threaded concurrent scenarios, and also has lower latency. For details, see the performance of the asynchronous logging tool. In addition, Log4j 2 is significantly faster than Log4j 1.x in many key areas and has similar performance to Logback in most environments. See Performance analysis for more information.

Support for multiple apis

  • Log4j 2 provides the best performance and supports SLF4J and Commons Logging apis.

Automatically reloads the configuration

  • Like Logback, Log4j 2 can be automatically reloaded based on configuration changes. In contrast, the log still works during a configuration reload without losing records.

Advanced filtering

  • Like Logback, Log4j 2 can be filtered based on context data, tags, regular expressions, or other components in log events. Filters can be specified before all events are passed to Logger or Appender. Filters can also be associated with loggers. Unlike Logback, you can use a generic filter class in these environments.

Plug-in architecture

  • Log4j uses the plug-in pattern to configure components. So you don’t need to code to create or configure appenders, layouts, patterns Converter, etc. Log4j automatically recognizes the plug-in and invokes it as configured.

Support properties

  • You can reference properties in a configuration file, which Log4j either replaces directly or passes to an underlying component and resolves dynamically. Properties come from values defined elsewhere, whether in configuration files, system properties, environment variables, thread context maps, or event data. Users can even customize how properties are provided by adding their own Lookup plug-in.

Support for Java 8 Lambda expressions

  • Previously, if constructing a log message was complex and time consuming, you would normally manually check whether the required log level was enabled before constructing the message. Log4j’s support for lambda expressions will benefit client code using Java 8. Because lambda expressions are not evaluated if Log4j is not enabled for the required logging level, the same functionality is achieved with less code.

Configuration requirements

Log4j 2.4 and later requires Java 7 or later, and 2.0-Alpha1 to 2.3 requires Java 6 or later. Some features require additional dependencies that are documented in the feature documentation.

news

Log4j 2 is now available for production. The Log4j 2 API is not compatible with Log4j 1.x, but an adapter is available to allow applications to continue using the Log4j 1.x API. Adapters for Apache Commons Logging and SLF4J are also available.

documentation

  • Web and PDF versions of the user guide
  • Download address
  • API
  • javadoc
  • FAQ
  • Related articles

The official website: logging.apache.org/log4j/2.x/ open source address: github.com/apache/logg…