Force slf4j to use logback

后端 未结 2 614
南方客
南方客 2021-02-05 02:41

Is there anyway to force slf4j to use specific logging provider (logback in my case)? As in their docs:

Multiple bindings were found on the class path

相关标签:
2条回答
  • 2021-02-05 02:49

    Generally your own code is at the beginning of the classpath. Because of this, one way to do it is to create your own org.slf4j.impl.StaticLoggerBinder class:

    package org.slf4j.impl;
    
    import org.slf4j.ILoggerFactory;
    import org.slf4j.spi.LoggerFactoryBinder;
    
    /**
     * Force tests to use JDK14 for logging.
     */
    @SuppressWarnings("UnusedDeclaration")
    public class StaticLoggerBinder implements LoggerFactoryBinder {
        private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
    
        public static String REQUESTED_API_VERSION = "1.6";
    
        public static final StaticLoggerBinder getSingleton() {
          return SINGLETON;
        }
    
        private StaticLoggerBinder() {
        }
    
        @Override
        public ILoggerFactory getLoggerFactory() {
            return new JDK14LoggerFactory();
        }
    
        @Override
        public String getLoggerFactoryClassStr() {
            return "org.slf4j.impl.JDK14LoggerFactory";
        }
    }
    
    0 讨论(0)
  • 2021-02-05 03:04

    Other than cleaning up your class path, there is no way to force SLF4J to bind with a given implementation.

    0 讨论(0)
提交回复
热议问题