Maven, Ivy, Gradle, and SBT Artifacts

Log4j 2 is broken up in an API and an implementation (core), where the API provides the interface that applications should code to. Strictly speaking Log4j core is only needed at runtime and not at compile time.

However, below we list Log4j core as a compile time dependency to improve the startup time for custom plugins as it provides an annotation processor that generates a metadata file to cache plugin information as well as the necessary code to compile against to create custom plugins.

Using Log4j in your Apache Maven build

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

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.21.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.21.0</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.

ivy.xml

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

Using Log4j in your Gradle build

To build with Gradle, add the dependencies listed below to your build.gradle file.

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-api:2.21.0'
  implementation 'org.apache.logging.log4j:log4j-core:2.21.0'
}

Using Log4j in your SBT build

To build with SBT, add the dependencies listed below to your build.sbt file.

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-api" % "2.21.0"
libraryDependencies += "org.apache.logging.log4j" % "log4j-core" % "2.21.0"

Bill of Material

To keep your Log4j module versions in sync with each other, a BOM pom.xml file is provided for your convenience. To use this with Maven, add the dependency listed below to your pom.xml file. When you specify the version identifier in this section, you don't have to specify the version in your <dependencies/> section.

pom.xml

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-bom</artifactId>
      <version>2.21.0</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

Gradle 5.0+ supports importing Maven BOM as a platform to align dependency versions.

build.gradle

dependencies {
  implementation platform('org.apache.logging.log4j:log4j-bom:2.21.0')

  implementation 'org.apache.logging.log4j:log4j-api'
  runtimeOnly 'org.apache.logging.log4j:log4j-core'
  // etc.
}

To use this with Gradle 2.8-4.10, an additional Gradle plugin is required for dependency management functionality.

build.gradle

plugins {
  id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

dependencyManagement {
  imports {
    mavenBom 'org.apache.logging.log4j:log4j-bom:2.21.0'
  }
}

dependencies {
  // For Gradle 2.8-3.3 use compile/runtime instead of implementation/runtimeOnly
  implementation 'org.apache.logging.log4j:log4j-api'
  runtimeOnly 'org.apache.logging.log4j:log4j-core'
  // etc.
}

Optional Components

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

Log4j 1.x API Bridge

If existing components use Log4j 1.x and you want to have this logging routed to Log4j 2, then remove any log4j 1.x dependencies and add the following.

pom.xml

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

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-1.2-api" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-1.2-api:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-1.2-api" % "2.21.0"

Apache Commons Logging Bridge

If existing components use Apache Commons Logging 1.x and you want to have this logging routed to Log4j 2, then add the following but do not remove any Commons Logging 1.x dependencies.

pom.xml

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

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-jcl" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-jcl:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-jcl" % "2.21.0"

SLF4J Bridge

If existing components use SLF4J and you want to have this logging routed to Log4j 2, then add the following but do not remove any SLF4J dependencies.

pom.xml

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

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-slf4j-impl" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.21.0"

JUL Adapter

If existing components use Java Util Logging and you want to have this logging routed to Log4j 2, then add the following.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jul</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-jul" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-jul:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-jul" % "2.21.0"

Web Servlet Support

In order to properly support and handle the ClassLoader environment and container lifecycle of a web application, an additional module is required. This module is only required at runtime. In addition, if you're using servlets in an OSGi environment, make sure your preferred version of the servlet API is already available (e.g., if you want to use 3.0, but you've also got 2.5 loaded, make sure both are loaded).

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-web" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-web:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-web" % "2.21.0"

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.

pom.xml

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

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-taglib" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-taglib:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-taglib" % "2.21.0"

Apache Flume Appender

The Flume Appender allows applications to send events to Flume Agents.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-flume-ng</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-flume-ng" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-flume-ng:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-flume-ng" % "2.21.0"

Log4j to SLF4J Adapter

The Log4j 2 to SLF4J Adapter allows applications coded to the Log4j 2 API to be routed to SLF4J. Use of this adapter may cause some loss of performance as the Log4j 2 Messages must be formatted before they can be passed to SLF4J. The SLF4J Bridge must NOT be on the class path when this is in use.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-to-slf4j" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-to-slf4j:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-to-slf4j" % "2.21.0"

CouchDB

If your configuration uses the NoSQL CouchDB appender, then add the following.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-couchdb</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-couchdb" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-couchdb:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-couchdb" % "2.21.0"

MongoDB

If your configuration uses the NoSQL MongoDB appender, then add the following.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-mongodb</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-mongodb" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-mongodb:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-mongodb" % "2.21.0"

Cassandra

If your configuration uses the Cassandra appender, then add the following.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-cassandra</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-cassandra" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-cassandra:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-cassandra" % "2.21.0"

IO Streams

Log4j IO Streams allow applications to have data that is written to an OutputStream or a Writer be redirected to a Logger, or have data that is read from an InputStream or a Reader be wiretapped by a Logger. To use IO Streams, add the following.

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-iostreams</artifactId>
    <version>2.21.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-iostreams" rev="2.21.0" />
</dependencies>

build.gradle

dependencies {
  implementation 'org.apache.logging.log4j:log4j-iostreams:2.21.0'
}

build.sbt

libraryDependencies += "org.apache.logging.log4j" % "log4j-iostreams" % "2.21.0"

Snapshot builds

You can access the latest development snapshot by using the Maven repository https://repository.apache.org/snapshots and the current SNAPSHOT version. Generally, the main branch will use the next patch version as its snapshot version regardless of what the next actual version of Log4j will be. For example, if the latest release were 2.0, then main would be using the version 2.0.1-SNAPSHOT. Always verify with pom.xml in the main branch as described in the source repository page.

Maven

Maven users can add the following to their pom.xml to enable snapshots:

<repositories>
  <repository>
    <id>apache.snapshots</id>
    <name>Apache Snapshot Repository</name>
    <url>https://repository.apache.org/snapshots</url>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>

Gradle

Gradle users can add the following to their build.gradle to enable snapshots:

repositories {
  mavenCentral()
  maven { url 'https://repository.apache.org/snapshots' }
}

SBT

SBT users can add the following to their build.sbt to enable snapshots:

resolvers += "Apache Snapshot Repository" at "https://repository.apache.org/snapshots"