2017-03-07 85 views
0

實際上,我需要一個偵聽器來持續檢查日誌配置。 我想監聽器檢查所有記錄器類,如果更改任何配置,它不允許。如何使用偵聽器來檢查記錄器配置?

三江源

+0

可能的[如何在運行時設置的logback配置文件http://stackoverflow.com/questions/30939814/how-to-set-logback-configuration-file-at-runtime – freedev

+2

可能重複的副本?](http://stackoverflow.com/questions/30939814/how-to-set-logback-configuration-file-at-runtime) –

回答

0

part of documentation解釋如何在運行時加載的結構。然後你可以將你的logback.xml文件嵌入到你的jar或war中,並作爲資源加載,請看下面的例子。

InputStream is = YourClass.class.getResourceAsStream("embedded-logback-log.xml"); 

LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); 

try { 
    JoranConfigurator configurator = new JoranConfigurator(); 
    configurator.setContext(context); 
    // Call context.reset() to clear any previous configuration, e.g. default 
    // configuration. For multi-step configuration, omit calling context.reset(). 
    context.reset(); 

    // Here you load the new configuration 
    configurator.doConfigure(is); 

} catch (JoranException je) { 
    // StatusPrinter will handle this 
} 
StatusPrinter.printInCaseOfErrorsOrWarnings(context); 

logger.info("Entering application."); 
+0

Thankyou,通過使用JoranConfigurator,我可以設置強制類來登錄「信息」級別? –

+0

你可以加載(或撰寫)一個新的配置,其中更改日誌級別爲「信息」或任何你想要的。 – freedev

+0

謝謝,我改變了我的問題。我需要一個監聽器來連續檢查我的日誌配置。 –

相關問題