When using SLF4J Test with Springboot, it's properly understood that SLF4J can only make a non-overlapping packages. As such, Springboot SLF4J related package such as logback-classic has to be excluded for the testing purposes, such as shown in the following snippet.
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- slf4j-test -->
<dependency>
<groupId>uk.org.lidalia</groupId>
<artifactId>slf4j-test</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
In running the test cases manually, it works just fine and the test are successful. But in order to build and release the whole project, it appears that my springboot can't run, as shown the following when running with mvn spring-boot:run
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
Oct 28, 2019 2:30:04 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Oct 28, 2019 2:30:04 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.22]
Oct 28, 2019 2:30:04 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
It appears that there is some contradiction. When i included the logback-class back in the springboot-starter, then i can run the whole project but will fail the test (due to duplicates of logback-classic). But the test will fail and it will fail basically because the getLoggingEvents
of my TestLogger returns null
instead of something (also due to duplicates of logback-class (see below))
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/user.local/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/user.local/.m2/repository/uk/org/lidalia/slf4j-test/1.1.0/slf4j-test-1.1.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Any comment on this? Or did i miss something?