Close

Log4j 2 features and a quick start example

[Last Updated: Sep 21, 2017]

Log4j 2 is an upgrade to Log4j, with following main features:

  • API and implementation separation:

    With this separation we can switch a different implementation during loading time without breaking the code which uses Log4j API.

  • Async logging:

    Low latency, lock-free Async logging based on the LMAX Disruptor library. This improves performance. By default Async logging is not enabled.

  • Automatic Configuration reloading:

    Reloading of modified configuration during runtime without losing logging events.

  • Components:

    Log4j 2 has similar hierarchy based components as Log4j (Logger, Appender, Layout). Additionally it introduces Filter which allows more flexibility than filtering just based on levels and logger names. Appenders and Loggers both can have Filters.

  • SLF4J adapter:

    With the log4j-to-slf4j adapter, we can use any logger implementation with Log4j 2 API.

  • Other API support:

    Log4j 2 provides support for the Log4j 1.2, SLF4J, Commons Logging and java.util.logging (JUL) APIs, that means we can use these API with Log4j 2 implementation.

  • Plugin Architecture:

    Log4j 2 uses the plugin pattern to configure components.

  • Configuration options:

    Other than new XML/properties/programmatic configurations, we can also use JSON or YAML configuration files.

Quick getting started examples

pom.xml

<dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-core</artifactId>
   <version>2.8.2</version>
</dependency>
<dependency>
   <groupId>org.apache.logging.log4j</groupId>
   <artifactId>log4j-api</artifactId>
   <version>2.8.2</version>
</dependency>

Example with default configuration

If we don't provide any configuration files, the default configuration is used with console as output and Level.ERROR:

package com.logicbig.example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Example {

  final static Logger logger = LogManager.getLogger(Example.class);

  public static void main(String[] args) throws InterruptedException {
      logger.info("a test info message");
      logger.error("a test error message");
  }
}
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
16:00:45.660 [main] ERROR com.logicbig.example.Example - a test error message

With log4j2.properties configuration

src/main/resources/log4j2.properties

#appenders references
appenders=xyz

#defining xyz
appender.xyz.type = Console
appender.xyz.name = myOutput
appender.xyz.layout.type = PatternLayout
appender.xyz.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n

rootLogger.level = info

#rootLogger.appenderReferences 
rootLogger.appenderRefs = abc

#assigning rootLogger appender to myOutput
rootLogger.appenderRef.abc.ref = myOutput
package com.logicbig.example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Example {
  final static Logger logger = LogManager.getLogger(Example.class);

  public static void main(String[] args) {
      logger.info("a test info message");
      logger.error("a test error message");
  }
}

Output

17-06-08 21:18:07:663 INFO  Example:10 - a test info message
17-06-08 21:18:07:665 ERROR Example:11 - a test error message

Example Project

Dependencies and Technologies Used:

  • log4j-core 2.8.2: The Apache Log4j Implementation.
  • log4j-api 2.8.2: The Apache Log4j API.
  • JDK 1.8
  • Maven 3.3.9

Log4j 2 Getting Started Example Select All Download
  • log4j2-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Example.java
          • resources

    See Also