简体   繁体   中英

How to detect missing LOG4J2 config?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM