Maven,Ivy,Gradle 和 SBT 工件

Log4j 2 在一个 API 和一个实现(核心)中进行了分解,其中 API 提供了应用程序应编写代码的接口。严格来说,Log4j 内核仅在运行时才需要,而在编译时则不需要。

但是,下面我们将 Log4j 内核列为编译时间依赖项,以缩短custom plugins的启动时间,因为它提供了 Comments 处理器,该处理器生成元数据文件来缓存插件信息以及根据其进行编译以创建自定义插件的必需代码。

在您的 Apache Maven 版本中使用 Log4j

要使用Apache Maven进行构建,请将下面列出的依赖项添加到 pom.xml 文件中。

pom.xml

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

在您的 Apache Ivy 构建中使用 Log4j

要使用Apache Ivy进行构建,请将下面列出的依赖项添加到 ivy.xml 文件中。

ivy.xml

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

在您的 Gradle 版本中使用 Log4j

要使用Gradle进行构建,请将下面列出的依赖项添加到 build.gradle 文件中。

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
  compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
}

在您的 SBT 版本中使用 Log4j

要使用SBT进行构建,请将下面列出的依赖项添加到 build.sbt 文件中。

build.sbt

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

物料 Lists

为了使 Log4j 模块版本彼此保持同步,为了方便起见,提供了 BOM BOM pom.xml 文件。要将它与Maven一起使用,请将下面列出的依赖项添加到 pom.xml 文件中。在此部分中指定版本标识符时,不必在\ 部分中指定版本。

pom.xml

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

要将其与 Gradle 一起使用,则需要附加的Gradle plugin来进行依赖项 Management 功能。

build.gradle

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

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

dependencies {
  compile 'org.apache.logging.log4j:log4j-api'
  compile 'org.apache.logging.log4j:log4j-core'
  // etc.
}

Optional Components

Log4j 2.x 包含可以包含在应用程序中的几个可选组件。

Log4j 1.x APIbridge

如果现有组件使用 Log4j 1.x,并且您希望将此日志记录路由到 Log4j 2,则删除所有 log4j 1.x 依赖项并添加以下内容。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.13.3'
}

build.sbt

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

Apache Commons Logging Bridge

如果现有组件使用 Apache Commons Logging 1.x,并且您希望将此日志记录路由到 Log4j 2,则添加以下内容,但不要删除任何 Commons Logging 1.x 依赖项。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.13.3'
}

build.sbt

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

SLF4J Bridge

如果现有组件使用 SLF4J,并且您希望将此日志记录路由到 Log4j 2,则添加以下内容,但不要删除任何 SLF4J 依赖项。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.13.3'
}

build.sbt

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

JUL Adapter

如果现有组件使用 Java Util 日志记录,并且您希望将此日志记录路由到 Log4j 2,则添加以下内容。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-jul', version: '2.13.3'
}

build.sbt

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

Web Servlet 支持

为了正确地支持和处理 Web 应用程序的 ClassLoader 环境和容器生命周期,需要一个附加模块。仅在运行时需要此模块。另外,如果您在 OSGi 环境中使用 servlet,请确保已使用您首选的 servlet API 版本(例如,如果您想使用 3.0,但同时也加载了 2.5,请确保两个都已加载) )。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.13.3'
}

build.sbt

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

Tag Library

Log4j 日志标记库创建了在 JSP 中插入日志语句的功能,而无需使用 Java 脚本。它使用标准的 Log4j 2 API 根据您的 Log4j 配置记录消息。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-taglib', version: '2.13.3'
}

build.sbt

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

Apache Flume Appender

Flume Appender 允许应用程序将事件发送到 Flume Agents。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-flume-ng', version: '2.13.3'
}

build.sbt

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

Log4j 到 SLF4J 适配器

Log4j 2 到 SLF4J 适配器允许将编码为 Log4j 2 API 的应用程序路由到 SLF4J。使用此适配器可能会导致性能下降,因为 Log4j 2 消息必须先格式化,然后才能传递给 SLF4J。使用 SLF4Jbridge 时,不得将其放在 Classpath 上。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-to-slf4j', version: '2.13.3'
}

build.sbt

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

CouchDB

如果您的配置使用 NoSQL CouchDB 附加器,则添加以下内容。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-couchdb', version: '2.13.3'
}

build.sbt

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

MongoDB

如果您的配置使用 NoSQL MongoDB 附加程序,则添加以下内容。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-mongodb', version: '2.13.3'
}

build.sbt

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

Cassandra

如果您的配置使用 Cassandra 附加程序,则添加以下内容。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-cassandra', version: '2.13.3'
}

build.sbt

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

IO Streams

Log4j IO 流允许应用程序将写入 OutputStream 或 Writer 的数据重定向到 Logger,或从 Logger 窃听从 InputStream 或 Reader 读取的数据。要使用 IO 流,请添加以下内容。

pom.xml

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

ivy.xml

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

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-iostreams', version: '2.13.3'
}

build.sbt

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

Scala API

Logger API 的便捷 Scala 包装器。 SBT 用户可以将以下内容添加到他们的 build.sbt 中:

build.sbt

libraryDependencies += "org.apache.logging.log4j" %% "log4j-api-scala" % "11.0"

Maven,Ivy 和 Gradle 用户需要将 Scala 版本添加到工件名称。

Scala 2.12 用户可以使用以下内容:

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api-scala_2.12</artifactId>
    <version>11.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-api-scala_2.12" rev="11.0" />
</dependencies>

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api-scala_2.12', version: '11.0'
}

Scala 2.11 用户可以使用以下内容:

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api-scala_2.11</artifactId>
    <version>11.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-api-scala_2.11" rev="11.0" />
</dependencies>

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api-scala_2.11', version: '11.0'
}

Scala 2.10 用户可以使用以下内容:

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api-scala_2.10</artifactId>
    <version>11.0</version>
  </dependency>
</dependencies>

ivy.xml

<dependencies>
  <dependency org="org.apache.logging.log4j" name="log4j-api-scala_2.10" rev="11.0" />
</dependencies>

build.gradle

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api-scala_2.10', version: '11.0'
}

Snapshot builds

您可以使用 Maven 存储库 https://repository.apache.org/snapshots 和当前的 SNAPSHOT 版本访问最新的开发快照。通常,无论 Log4j 的下一个实际版本是什么,master 分支都将使用下一个补丁程序版本作为其快照版本。例如,如果最新版本为 2.0,则 master 将使用 2.0.1-SNAPSHOT 版本。始终按照源存储库页面中的说明在 master 分支中使用 pom.xml 进行验证。

Maven

Maven 用户可以将以下内容添加到其 pom.xml 中以启用快照:

<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 用户可以将以下内容添加到他们的 build.gradle 中以启用快照:

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

SBT

SBT 用户可以将以下内容添加到他们的 build.sbt 中以启用快照:

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