SLF4J警告 - 记录器将无法正常工作,因为它们是在初始化阶段创建的

时间:2018-01-15 18:24:09

标签: java logback slf4j dropwizard

我在dropwizard应用程序中使用slf4j和logback。在应用程序初始化期间,我看到如下的日志

SLF4J: The following loggers will not work because they were created
SLF4J: during the default configuration phase of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger
SLF4J: com.vnera.healthandmetrics.VneraMetrics

我在我的dropwizard应用程序中使用logback.xml。我的代码流程如下所示

public class VneraMetrics {
   private static final Logger logger = LoggerFactory.getLogger(VneraMetrics.class);
 ...
 // This method is getting called from Service.run() during the dropwizard application initialization
 public String getSomeValue() {
     // logger is not accessed from this function
     return "Some initialized value";
 }
}

public class Service extends Application<Conf> {
   public static final Logger logger = LoggerFactory.getLogger(Service.class);
   public static void main(String args[]) {
      logger.info("Some logs");
      Service service = new Service();
      service.run(dropWizardArgs);
      Utils.reloadLogger();
   }
}

Utils.reloadLogger()正在加载如此处讨论的loggback配置

public static void reloadLogger() {
        String loggingConfig = System.getProperty("logback.configurationFile");
        if(loggingConfig == null) {
            System.out.println("Logging Config is null");
        }

        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        loggerContext.reset();
        JoranConfigurator configurator = new JoranConfigurator();

        try {
            InputStream configStream = FileUtils.openInputStream(new File(loggingConfig));
            configurator.setContext(loggerContext);
            configurator.doConfigure(configStream); // loads logback file
            configStream.close();
            System.out.println("Loaded configuration file");
        } catch (JoranException | IOException e) {
            e.printStackTrace();
            System.out.println("Failed to log configuration file");
            System.exit(1);
        }
    }
}

版本

  • Logback - 1.2.3
  • Dropwizard - 1.0.2

有人可以告诉我SLF4J警告的含义是什么吗?我看过substituteLogger页面,但是这没有提到如何解决这个问题?尝试在VneraMetrics中使用记录器时,它不会打印任何内容。

0 个答案:

没有答案