Apache logging services logo Apache log4j logo

Building and Using Log4j 2.x

Building

Log4j 2.x is hosted in the Apache Software Foundation's subversion repository. Details on obtaining the most current source code can be found at Log4j Source Repository. The source from the latest release may be obtained by downloading it using the instructions at Log4j Downloads.

Log4j 2.x uses Maven 2 or 3 as its build tool. To build Log4j simply run "mvn install" from the root directory. Then to build the site, run "mvn site". To rebuild only what's changed and execute the tests, run "mvn test". To rebuild from scratch, add "clean", for example: "mvn clean test".

Using Log4J on your classpath

To use Log4j 2 in your application make sure that both the API and Core jars are in the application's classpath. Add the dependencies listed below to your classpath.

  • log4j-api-2.0-beta5.jar
  • log4j-core-2.0-beta5.jar

You can do this from the command line or a manifest file.

Using Log4J in your Apache Maven build

To build with Apache Maven, add the dependencies listed below to your pom.xml file.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta5</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta5</version>
  </dependency>
</dependencies>

Using Log4J in your Apache Ivy build

To build with Apache Ivy, add the dependencies listed below to your ivy.xml file.

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-api" rev="2.0-beta5" />
  <dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.0-beta5" />
</dependencies>

Optional Components

Log4j 2.x contains several optional components that can be included in an application.

Log4j 1.x

If there are existing components that are written to use Log4j 1.x and it is desired to have this logging routed to Log4j 2 then remove any log4j 1.x dependencies and add the following.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.0-beta5</version>
  </dependency>
</dependecies>

Apache Commons Logging

Commons Logging is used in many components as a way of letting applications choose the specific logging implementation. To route logging from those components into Log4j 2 include the following dependency and do not remove the Commons Logging jar.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>2.0-beta5</version>
  </dependency>
</dependencies>

SLF4J

SLF4J is another popular logging API that may be bound to Log4j 2 by including the following dependency along with the SLF4J dependencies.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.0-beta5</version>
  </dependency>
</dependencies>

Tag Library

The Log4j Log Tag Library creates the capability of inserting log statements in JSPs without the use of Java scripting. It uses the standard Log4j 2 API to log messages according to your Log4j configuration.

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-taglib</artifactId>
    <version>2.0-beta5</version>
  </dependency>
</dependencies>