How to detect missing LOG4J2 config?

自作多情 提交于 2019-12-31 00:46:37

问题


I would like to be able to detect when the app is missing a LOG4J2.XML configuration file, and in that case set some other defaults than what LOG4J2 has otherwise.

Basically, I would like to run this code if a config file isnt found:

    // A default configuration that shows ALL Logger output, without a XML config!
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); 
    loggerConfig.setLevel(Level.ALL);
    ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfig.        

How can I detect that LOG4J2 failed to load a configuration?


回答1:


Seems like ...

if (config instanceof DefaultConfiguration)

...would be enough, since DefaultConfiguration is used whenever there is nothing else available, see the documentation for Log4j2 configuration:

If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.



来源:https://stackoverflow.com/questions/32250527/how-to-detect-missing-log4j2-config

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!