什么是get4urrentLoggers在log4j2中的模拟

Yur*_*riR 9 java configuration logging log4j log4j2

如何获得log4j2中使用的所有记录器?在log4j中我可以使用如下所述的getCurrentLoggers:使用的记录器数量

zhu*_*ian 6

获取 log4j2 中使用的所有记录器:

LoggerContext logContext = (LoggerContext) LogManager
                .getContext(false);
Map<String, LoggerConfig> map = logContext.getConfiguration()
                .getLoggers();
Run Code Online (Sandbox Code Playgroud)

注意力:

使用 org.apache.logging.log4j.core.LoggerContext

不是 org.apache.logging.log4j.spi.LoggerContext

  • 在 2.6.2 中,没有 `LoggerContext.getConfiguration()` (3认同)

Yur*_*riR 5

看起来我已经找到了方法:

File configFile = new File("c:\\my_path\\log4j2.xml");
LoggerContext loggerContext = Configurator.initialize("my_config", null, configFile.toURI());
Configuration configuration = loggerContext.getConfiguration();
Collection<LoggerConfig> loggerConfigs = configuration.getLoggers().values();
Run Code Online (Sandbox Code Playgroud)