Video in progress

Strong Middle 🧭 Design patterns

What patterns is SLF4J built on, and what happens when two logging libraries clash on the classpath?

SLF4J is a facade: your code knows only its API, and Logback or Log4j2 does the actual writing. Bridges like jul-to-slf4j and log4j-to-slf4j are adapters that route other logging APIs into SLF4J. If a bridge and a provider are paired wrongly, logs disappear or an infinite loop appears.

📖 Answer

Your code imports only org.slf4j.Logger. The classpath decides which implementation runs: SLF4J 2.x finds the provider through ServiceLoader (in 1.x this was the StaticLoggerBinder class). If no provider is found, you get the "No SLF4J providers were found" warning and logs silently go to a NOP logger.

Spring Boot's spring-boot-starter-logging brings the usual set: logback-classic (the provider), jul-to-slf4j and log4j-to-slf4j (bridges). So a library that uses the JUL or Log4j2 API also ends up writing through Logback, and all logs come out in one format.

Problems usually come from transitive dependencies:

  • two providers at the same time (logback-classic and slf4j-reload4j): SLF4J picks one and prints a warning, and your logback.xml may stop working;
  • a bridge and a provider pointing in opposite directions: if log4j-over-slf4j (Log4j → SLF4J) and slf4j-reload4j (SLF4J → Log4j) are both present, calls go in a circle and end in a StackOverflowError. SLF4J detects this and fails at startup.

If you want to move to Log4j2, you exclude spring-boot-starter-logging and add spring-boot-starter-log4j2. To check, mvn dependency:tree | grep -i -E "slf4j|logback|log4j" is enough.