Spring

 View Only

 How to use different logger implementation in Spring Boot based on SLF4J

Nigel Longton's profile image
Nigel Longton posted Nov 26, 2018 07:25 PM

We have a test logger that implements the SLF4J interfaces of ILoggerFactory and Logger. However SpringApplication is throwing an Assert as it want the ILoggerFactory to be implemented by LogBack's LoggerContext which due to what seems a type return error we can't implement from. 

Are there other ways to insert an SLF4J based logger implementation into SpringBoot?

Daniel Mikusa's profile image
Daniel Mikusa

Spring Boot uses Logback directly by default. It can also be configured to use Log4j 2. It sounds like you want to use a custom logging implementation instead of Logback or Log4j 2. To do that, I think you'd need to write your own LoggingSystem and configure your app to use that LoggingSystem.

 

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration

 

More details on LoggingSystem here.

 

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

 

and of course you can reference the Logback / Log4j 2 implementations of LoggingSystem here.

 

https://github.com/spring-projects/spring-boot/tree/2.0.x/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging

 

Hope that helps!

Nigel Longton's profile image
Nigel Longton

I found a solution - slf4j != logback. I created an implementation of Skf4jLoggingSystem that returned our logger factory and life is good again.