我可以通过这种方式以编程方式更改Log4j 2的日志级别,并且运行正常,
LoggerContext context = (LoggerContext) LogManager.getContext(false);
AbstractConfiguration absConfig = (AbstractConfiguration) context.getConfiguration();
LoggerConfig currentLoggerConfig = absConfig.getLogger(loggerName);
LoggerContext context = (LoggerContext) LogManager.getContext(false);
AbstractConfiguration absConfig = (AbstractConfiguration) context.getConfiguration();
LoggerConfig currentLoggerConfig = absConfig.getLogger("");
if (currentLoggerConfig != null) {
currentLoggerConfig.setLevel(Level.ERROR);
} else {
LoggerConfig loggerConfig = new LoggerConfig(loggerName, Level.ERROR, true);
absConfig.addLogger(loggerName, loggerConfig);
}
context.updateLoggers();
现在我想将根级别重置为原始状态而不重新启动服务器。我试过" LoggerContext.reconfigure()" ,但我只看到ERROR而不是INFO(这是配置文件中的默认值)。
有人可以帮我解决如何在配置文件(log4j2.xml)中将级别设置回原始状态?