1

I want to use commons-logging API with log4j 2.

My classes have the following code get the logger ( the 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 am facing problem, similar to what described in the following question : I'm getting "NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil"

So the short question what is the maven dependencies I have to use, to get the commons-logging api work with latest version of log4j2

Following is my non-working 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>
  • Can you add the list and versions of your Log4j dependencies? Do you use any JCL replacements (`spring-jcl`, `jcl-over-slf4j`, etc.) or is it vanilla `commons-logging`? – Piotr P. Karwasz Dec 28 '21 at 05:07
  • I have added pom.xml I am using. Please guide. Thanks – Vishwanath Washimkar Dec 28 '21 at 05:21
  • 1
    Kudos on `2.17.0` in your pom.xml. If you're using log4j, you *MUST* use 2.15 or higher: https://www.kaspersky.com/blog/log4shell-critical-vulnerability-in-apache-log4j/43124/ ALSO: Look here: https://stackoverflow.com/a/41475146/421195. You might not need commons logging at all; or you might need to add the adapter (jcl) bridge – paulsm4 Dec 28 '21 at 05:32

1 Answers1

2

The vanilla Commons Logging has a fixed list of logging implementations it can discover (cf. documentation). Log4j 1.x is among them, but Log4j 2.x is not.

Therefore you need to add the Log4j Commons Logging Adapter to your classpath:

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

Under the hood this contains an alternative implementation of LogFactory, which will be automatically discovered and used through the ServiceLoader mechanism.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • If this answers your question, you should consider accepting it (the checkmark sign near the answer). You should do the same in all your other questions, even those you answered yourself. – Piotr P. Karwasz Dec 28 '21 at 06:41