如何检测丢失的LOG4J2配置? [英] How to detect missing LOG4J2 config?

查看:237
本文介绍了如何检测丢失的LOG4J2配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检测到应用程序何时缺少LOG4J2.XML配置文件,并且在这种情况下设置一些其他默认值,而不是LOG4J2的其他默认值。

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.        

如何检测到LOG4J2无法加载配置?

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

推荐答案

似乎... ...

if (config instanceof DefaultConfiguration)

...就足够了,因为只要没有其他可用的东西就使用DefaultConfiguration,请参阅有关Log4j2配置的文档

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


如果找不到配置文件,将使用 DefaultConfiguration
。这将导致日志记录输出进入控制台。

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

这篇关于如何检测丢失的LOG4J2配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆