如何在最新版本的log4j2中使用commons日志api



我想将commons-logging API与log4j2一起使用。

我的类有以下代码获取记录器(commons logging api(

package com.example.testwebapp;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Log4j2ConfigBuilder {

public static void main(String[] args) { 
getLog().debug("DEBUG log entryc 11111 ");
getLog().info("INFO log entry ");
getLog().error("ERROR log entry ");
getLog().warn("#############  WAR log entry ");
}
/**
* @return The logger for the class.
*/
private static Log getLog() {
return LogFactory.getLog(Log4j2ConfigBuilder.class);
}
}

我面临的问题类似于下面的问题:I';我得到了";NoClassDefFoundError:org/apache/logging/log4j/util/ReflectionUtil";

因此,简短的问题是,我必须使用什么样的maven依赖项,才能使commons日志api与最新版本的log4j2一起工作

以下是我的非工作pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>TestWebApp</artifactId>
<version>1.0-SNAPSHOT</version>
<name>TestWebApp</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.7.1</junit.version>
<log4j.version>2.17.0</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>

普通的Commons Logging有一个固定的日志实现列表,它可以发现(参见文档(。Log4j 1.x是其中之一,但Log4j 2.x不是。

因此,您需要将Log4j Commons Logging Adapter添加到类路径中:

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
</dependency>

在引擎盖下,它包含LogFactory的替代实现,它将通过ServiceLoader机制自动发现和使用。

最新更新