Peter Mel
Peter Mel

Reputation: 367

How can I make my Logger implementation be automatically injected by SLF4J in an application?

So I have my own implementation of org.slf4j.Logger called MyLogger. So far so good. I have an app that uses SLF4J + LOG4J. This is configured in the pom.xml.

My question is: How do I somehow make SLF4J take my Logger implementation and inject everywhere instead of using log4j?

I am looking for an example or explanation about how to switch between SLF4J implementations (log4j, logback, MyLogger, etc) without requiring any code changes, just pom.xml configurations or something else.

Upvotes: 2

Views: 444

Answers (1)

rdalmeida
rdalmeida

Reputation: 1853

SLF4J will try inject the first compatible Logger (org.slf4j.Logger) it finds in the classpath. For that to happen, you log implementation must also provide a LoggerFactory through the implementation of org.slf4j.ILoggerFactory that returns your logger. More info here: http://www.slf4j.org/faq.html#slf4j_compatible

Upvotes: 1

Related Questions