Home > Software design >  Why does spring-boot-starter-logging require a dependency on log4-to-slf4j
Why does spring-boot-starter-logging require a dependency on log4-to-slf4j

Time:02-19

I have a Spring Boot 2.6.3 project that uses spring-boot-starter. When I run the dependecy:tree goal in maven, I see that spring-boot-starter-logging depends on both logback and (indirectly) log4j. Why does spring-boot-starter-logging require a dependency on log4-to-slf4j?

enter image description here

CodePudding user response:

Spring Boot has bindings for all major logging frameworks. With a single configuration you can concentrate logs sent through SLF4J, Log4j 2.x API or java.util.logging.

Therefore the spring-boot-starter-logging provides:

  • a binding for SLF4J (logback-classic),
  • an implementation of the Log4j 2.x API (log4j-to-slf4j). Remark that this is not the standard Log4j 2.x Core implementation.
  • a handler for java.util.logging (jul-to-slf4j).

Remark that spring-boot-starter-log4j2 does the same thing and redirects the frameworks above to Log4j 2.x Core.

The big absent in this picture is Jakarta Commons Logging, which is only able to bind to java.util.logging (hence not directly to neither Logback nor Log4j 2.x Core). However spring-core depends on spring-jcl, which binds JCL directly to SLF4J or the Log4j 2.x API and can entirely replace the original JCL.

A fifth API (Log4j 1.x) was supported in Spring Boot 1.x.

  • Related