How to remove logback from library dependency while saving SLF4J?

In my Vaadin project, I have a dependency on a specific library. This library uses slf4j for logging. In the pom library, the logback slf4j binding is added as a run-time dependency.

<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency> 

In my application, I use log4j directly for logging. I want the logs added by the library to log log4j.

For this, I added the following to my pom to enable slf4j log4j binding

  <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> 

However, slf4j complains that it has detected several bindings.

 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/program_files/apache-tomcat-8.0.24/temp/0-ROOT/WEB-INF/lib/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/program_files/apache-tomcat-8.0.24/temp/0-ROOT/WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class] 

I checked the dependency tree of my application, which has the following values ​​for its dependency on logback. (The following is the only logback dependency)

 [INFO] | +- com.mycompany.mylib:libname:jar:1.1.0-SNAPSHOT:compile [INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.5:runtime [INFO] | | +- ch.qos.logback:logback-classic:jar:1.0.13:runtime [INFO] | | | \- ch.qos.logback:logback-core:jar:1.0.13:runtime [INFO] | | +- ch.qos.logback:logback-access:jar:1.0.13:runtime 

In addition, when I checked inside the WEB-INF\lib directory in my war file, I found the following banks.

 logback-access-1.0.13.jar logback-classic-1.0.13.jar logback-core-1.0.13.jar 

Why did the registry end in my lib directory? As I heard, runtime dependencies should not go into the libs directory.

How do I resolve this? The library was developed in my company, and I can ask the library developers to remove the necessary log runtime dependencies.

+5
source share
1 answer

Option 1: they change their pom.

The easiest way to fix this would be for library developers in your own company to mark logback as optional and require SLF4J as a compilation dependency explicitly. This is the correct, canonical way to make SLF4J in Maven. In other words:

 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> <optional>true</optional> </dependency> 

Option 2: you are reviewing your dependencies in your folder.

If you want to fix this yourself without going through them, you can use the exclusions tag when declaring your dependency. In other words, in your pom do:

 <dependency> <groupId>your.company</groupId> <artifactId>libraryname</artifactId> <version>${theirlibrary.version}</version> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> </exclusions> </dependency> 

You asked if there was a reason to hang directly with Logback; not at all, for the author of the library. Their pom configuration is probably only minor oversight on their part. There are some reasons that depend on logback specifically, but they are related to the launch (material with JoranConfigurator or StatusPrinter , something like that should not appear in the library. Other reasons for calling log classes include things like custom appenders , which, again, should not appear in the library, only a deployed application.

+11
source

All Articles