Can't exclude logback-classic dependency from

2019-08-15 19:27发布

问题:

I am working a number of projects that are using Dropwizard 0.8.2 with sfl4j-api 1.7.12 for logging. Our chosen implementation for slf4j is slf4j-log4j12, but we are having a problem with multiple implementations present in the project.

When trying to run it, it complains that multiple SLF4J bindings are present. One is ours, the other is logback-classic which is coming from the dropwizard-jackson dependency.

The problem is when we try to exclude logback-classic from the dependencies we get the following error when trying to start the server:

Exception in thread "main" java.lang.NoClassDefFoundError: ch/qos/logback/classic/filter/ThresholdFilter
    at io.dropwizard.Application.<clinit>(Application.java:20)
Caused by: java.lang.ClassNotFoundException: ch.qos.logback.classic.filter.ThresholdFilter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

Is there any way that people know of to use log4j with Dropwizard?

回答1:

I don't think you can remove logback from the runtime classpath and make dropwizard use some other logging implementation (without modifying dropwizard's code). As we can see, io.dropwizard.Application's static initializer calls io.dropwizard.logging.LoggingFactory.bootstrap(), and LoggingFactory directly uses classes from logback, so LoggingFactory class just won't initialize without logback.