C. Bilgin
C. Bilgin

Reputation: 65

MDC content is not printed via log4j2-jboss-logmanager on quarkus

I want to put a content into "org.jboss.logging.MDC" and make available in logging via log4j2, but value is not showable only for log4j2. I also tried "java.util.logging.Logger" and "org.jboss.logging.Logger". These can show MDC content fine in "log.info("test with log4j")" code line. Here is the classes and pom.xml, and pushed my issue in the repo as well https://github.com/cembilgin/quarkuslog4j2mdcissueapp I am fresh man in Quarkus. Any help is appreciated. Thanks.

Resource class=>

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.ThreadContext;
import org.jboss.logging.MDC;

@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");
    }

}

test class which is working fine. I am putting value in MDC and getting from ThreadContext org.jboss.logging.Log4j.LoggerProvider is selected as logging provider=>

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>

Somehow, log4j2 is not selected as logging provider. JBossLogManager always is selected even I explicitly set log4j2 with setting vm options as "-Dorg.jboss.logging.provider=log4j2" . After I deeply investigated, it tries log4j2 in org.jboss.logging.LoggerProviders, but cannot find "org.apache.logging.log4j.Logger" in classpath then throws exception and tries JBossLogManager. As seen I added "log4j2-jboss-logmanager" dependency in pom.xml which brings log4j-api library. Also, runtime log4j2 related class exist. It can be seen MDCLog4j2ResourceTest class that I checked classloader. Why can't it be found? Any ideas? log4j finder method

Upvotes: 0

Views: 321

Answers (0)

Related Questions