How to find which library slf4j has bound itself to?

前端 未结 4 718
一整个雨季
一整个雨季 2020-12-24 01:19

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

相关标签:
4条回答
  • 2020-12-24 01:27

    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.

    0 讨论(0)
  • 2020-12-24 01:35

    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 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
    
    0 讨论(0)
  • 2020-12-24 01:43

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

    0 讨论(0)
  • 2020-12-24 01:49

    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()
    ) 
    { ... }
    
    0 讨论(0)
提交回复
热议问题