How to find which library slf4j has bound itself to?

JavaLoggingLog4jSlf4j

Java Problem Overview


I am using slf4j for logging in my application. I get the purpose of slf4j. I would like to know how to find out which logging-library slf4j is currently binding to. I have log4j in my referenced libraries. I am assuming that slf4j has bound itself to log4j.

What I would like to know is, is there any way to explicitly confirm this binding?

Java Solutions


Solution 1 - Java

Just do what SLF4J does to discover the binding:

final StaticLoggerBinder binder = StaticLoggerBinder.getSingleton();

Now you can try to find out what is the actual implementation [tag:logback] in my case:

System.out.println(binder.getLoggerFactory());
System.out.println(binder.getLoggerFactoryClassStr());

This prints:

ch.qos.logback.classic.LoggerContext[default]
ch.qos.logback.classic.selector.DefaultContextSelector

Solution 2 - Java

The StaticLoggerBinder's getLoggerFactoryClassStr() method is probably what you're looking for.

Solution 3 - Java

Easy. Put a breakpoint on .. say.. LOG.info(...). Once debugger stops there, step into.. and viola.. you will find yourself in the code of the actual logger... say log4j or logback.. whatever.

Solution 4 - Java

It's possible to do this using the main slf4j public API (i.e. without the internal StaticLoggerBinder), e.g. to detect if slf4j has bpound to log4j2:

if ("org.apache.logging.slf4j.Log4jLoggerFactory".equals(
    org.slf4j.LoggerFactory.getILoggerFactory().getClass().getName()
) 
{ ... }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpsykeronView Question on Stackoverflow
Solution 1 - JavaTomasz NurkiewiczView Answer on Stackoverflow
Solution 2 - Javaleedm777View Answer on Stackoverflow
Solution 3 - JavaApurva SinghView Answer on Stackoverflow
Solution 4 - JavaBen SpillerView Answer on Stackoverflow