JUL to SLF4J Bridge

前端 未结 5 1396
小蘑菇
小蘑菇 2020-11-27 11:57

I\'m currently observing that a 3rd party library (namely restfb) is using java.util.logging and I\'m seeing those logs end up in STDOUT even though I don\'t have an SLF4J c

5条回答
  •  时光取名叫无心
    2020-11-27 12:14

    You need to call SLF4JBridgeHandler.install(). You also need to enable all log levels at the root logger (reason in excerpt below) in java.util.logging and remove the default console appender.

    This handler will redirect jul logging to SLF4J. However, only logs enabled in j.u.l. will be redirected. For example, if a log statement invoking a j.u.l. logger disabled that statement, by definition, will not reach any SLF4JBridgeHandler instance and cannot be redirected.

    The whole process can be accomplished like so

    import java.util.logging.Logger;
    import org.slf4j.bridge.SLF4JBridgeHandler;
    
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();
    Logger.getLogger("").setLevel(Level.FINEST); // Root logger, for example.
    

    You can set the level to something higher than finest for performance reasons, but you won't be able to turn those logs on without enabling them in java.util.logging first (for the reason mentioned above in the excerpt).

提交回复
热议问题