Connect commons-logging to slf4j-api
Asked Answered
E

1

6

Wanted to know the dependency that i should include to bridge commons-logging to slf4j-api.

My build is successful by adding below:

"org.apache.logging.log4j:log4j-api:2.12.1"
"org.apache.logging.log4j:log4j-core:2.12.1"
"org.apache.logging.log4j:log4j-1.2-api:2.12.1"
"org.slf4j:slf4j-api:${slf4j_version}"
"org.apache.logging.log4j:log4j-slf4j-impl:2.12.1"
"org.slf4j:slf4j-jcl:1.7.25"

But not able to startup my services :( Getting java.lang.ExceptionInInitializerError... Caused by: org.springframework.beans.factory.CannotLoadBeanClassException

Effendi answered 11/9, 2019 at 4:48 Comment(1)
JCL stands for Jakarta Commons Logging.Humidify
A
12

There are a few steps you need to take here. First, you should include the jcl-over-slf4j.jar file, for example:

<!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.28</version>
</dependency>

This JAR, if included at runtime, should receive all logging calls from anything using Jakarta commons logging, and then re-route it to your SLF4J facade (from which you may log with any implementation you want).

But, there is one additional step you should ideally take. You should change the dependency scope of commons-logging to provided in your Maven POM:

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>[1.0,)</version> <!-- include all possible versions -->
    <scope>provided</scope>   <!-- IMPORTANT -->
</dependency>

By making commons-logging provided, you are telling Maven to include it during build time for any component in your code which needs it (e.g. something like Spring), but to exclude it at runtime, in the final JAR output. Instead, the bridge JAR jcl-over-slf4j.jar mentioned above will be there at runtime. Spring, for example, will still be calling Jakarta logging at runtime, but that will really just be getting fed into the SLF4J facade, to end up with whatever logging implementation you provide.

Check the slf4j documentation which discusses a few of the things mentioned above.

Side note: You may want to run mvn dependency:tree on your project to verify that your Maven library configuration be correct. You should not see any other logging implementation other than the one you chose to use with SLF4J. In particular, commons-logging should only show up as provided, i.e. it should not be on your classpath for your JAR.

Acth answered 11/9, 2019 at 4:59 Comment(1)
Hey Tim! Thankyou so much for your such a prompt answer! Will analyze and test and let you know.Effendi

© 2022 - 2024 — McMap. All rights reserved.