Quarkus中log4j2-jboss-logmanager无法输出MDC内容求助
Quarkus中org.jboss.logging.MDC无法通过Log4j2输出的问题
我需要将内容存入org.jboss.logging.MDC并通过Log4j2输出,但Log4j2日志无法显示该MDC值;使用java.util.logging.Logger和org.jboss.logging.Logger时,log.info("test with log4j")能正常显示MDC内容。相关代码及配置如下:
资源类代码
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import lombok.log4j.Log4j2; import org.apache.logging.log4j.ThreadContext; import org.jboss.logging.MDC; import java.io.IOException; @Path("/hello") @Log4j2 public class MDCLog4j2Resource { @GET @Produces(MediaType.TEXT_PLAIN) public String hello() throws IOException { MDC.put("MDCTest", "MDCTest"); System.out.println("MDC value:" + MDC.get("MDCTest")); System.out.println("ThreadContext MDCTest value:" + ThreadContext.get("MDCTest")); System.out.println("log4j Logger class: " + getClass().getClassLoader() .getResources("org/apache/log4j/Logger.class")); System.out.println("log4j LogManager class: " + getClass().getClassLoader() .getResources("org/apache/logging/log4j.LogManager")); System.out.println("log42 AbstractLogger Logger: " + getClass().getClassLoader() .getResources( "org/apache/logging/log4j/spi.AbstractLogger")); log.info("test with log4j"); System.out.println("ThreadContext value :" + ThreadContext.get("MDCTest")); return ThreadContext.get("MDCTest"); } }
测试类代码
import org.apache.logging.log4j.ThreadContext; import org.jboss.logging.MDC; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class MDCLog4j2ResourceTest { @BeforeAll public static void setup() { System.setProperty("org.jboss.logging.provider", "log4j2"); } @Test void testMdc() throws ClassNotFoundException { MDC.put("test.key", "value"); Assertions.assertEquals("value", MDC.get("test.key")); Assertions.assertEquals("value", ThreadContext.get("test.key")); final ClassLoader cl = MDCLog4j2ResourceTest.class.getClassLoader(); Class.forName("org.apache.logging.log4j.Logger", true, cl); Class.forName("org.apache.logging.log4j.LogManager", true, cl); Class.forName("org.apache.logging.log4j.spi.AbstractLogger", true, cl); } }
pom.xml配置
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>quarkuslog4j2mdcissueapp</artifactId> <version>1.0-SNAPSHOT</version> <properties> <compiler-plugin.version>3.10.1</compiler-plugin.version> <maven.compiler.release>11</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id> <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id> <quarkus.platform.version>2.16.4.Final</quarkus.platform.version> <skipITs>true</skipITs> <surefire-plugin.version>3.0.0-M7</surefire-plugin.version> <cp.test.classes.dir>${project.build.directory}${file.separator}cp-test-classes</cp.test.classes.dir> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>${quarkus.platform.artifact-id}</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> <dependency> <groupId>org.jboss.logmanager</groupId> <artifactId>log4j2-jboss-logmanager</artifactId> <version>1.1.1.Final</version> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-logging-json</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus.platform.version}</version> <extensions>true</extensions> <executions> <execution> <goals> <goal>build</goal> <goal>generate-code</goal> <goal>generate-code-tests</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${compiler-plugin.version}</version> <configuration> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <systemPropertyVariables> <org.jboss.logging.provider>log4j2</org.jboss.logging.provider> <quarkus.log.level>DEBUG</quarkus.log.level> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> <classpathDependencyExcludes> </classpathDependencyExcludes> </configuration> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>${surefire-plugin.version}</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> <configuration> <systemPropertyVariables> <native.image.path>${project.build.directory}/${project.build.finalName}-runner </native.image.path> <org.jboss.logging.provider>log4j2</org.jboss.logging.provider> <quarkus.log.level>DEBUG</quarkus.log.level> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>native</id> <activation> <property> <name>native</name> </property> </activation> <properties> <skipITs>false</skipITs> <quarkus.package.type>native</quarkus.package.type> </properties> </profile> </profiles> </project>
排查情况
- 测试类运行正常,能在MDC存入值并从ThreadContext获取,且
org.jboss.logging.Log4j.LoggerProvider被选为日志提供者 - 即使通过VM选项设置
-Dorg.jboss.logging.provider=log4j2,Log4j2仍未被选为日志提供者,始终使用JBossLogManager - 跟踪发现
org.jboss.logging.LoggerProviders尝试加载Log4j2时,无法在类路径中找到org.apache.logging.log4j.Logger类,抛出异常后转而使用JBossLogManager - 已在pom.xml中添加
log4j2-jboss-logmanager依赖(该依赖会引入log4j-api库),且运行时Log4j2相关类确实存在(测试类中通过类加载器检查可验证)
疑问
为何org.jboss.logging.LoggerProviders加载Log4j2时找不到org.apache.logging.log4j.Logger类?
内容的提问来源于stack exchange,提问作者C. Bilgin
相关产品推荐
相关产品推荐

