Force slf4j to use logback

后端 未结 2 616
南方客
南方客 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";
        }
    }
    

提交回复
热议问题