简体   繁体   English

如何在运行时使用log4j2 api将loglevel从INFO设置为ERROR?

[英]how to set loglevel from INFO to ERROR using log4j2 api at runtime?

logger.setLevel() method is not available in log4j2 API. log4中没有logger.setLevel()方法。 So how to set log level at run time. 那么如何在运行时设置日志级别。

I'm not sure if this is the best way, but you set the level on org.apache.logging.log4j.core.config.LoggerConfig which you can get from the LoggerContext via the LogManager. 我不确定这是否是最佳方法,但您可以在org.apache.logging.log4j.core.config.LoggerConfig上设置级别,您可以通过LogManager从LoggerContext获取该级别。

Once set, you can update the loggers with the new configuration. 设置后,您可以使用新配置更新记录器。

As an example: 举个例子:

public static void main(String[] args) {
    Logger log = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    log.error("An error");
    log.debug("A debug");

    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration conf = ctx.getConfiguration();
    conf.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(Level.DEBUG);
    ctx.updateLoggers(conf);

    log.error("Another error");
    log.debug("Another debug");
}

Yields: 产量:

14:03:41.346 [main] ERROR  - An error
14:03:41.348 [main] ERROR  - Another error
14:03:41.348 [main] DEBUG  - Another debug

Credit to amcintosh, I wrapped their answer in a function: 感谢amcintosh,我把他们的答案包裹在一个函数中:

/** Override the logging level of a given logger, return the previous level */
public static Level setLevel(Logger log, Level level) {
  LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
  Configuration conf = ctx.getConfiguration();
  LoggerConfig lconf = conf.getLoggerConfig(log.getName());
  Level oldLevel = lconf.getLevel();
  lconf.setLevel(level);
  ctx.updateLoggers(conf);
  return oldLevel;
}

Despite amoe's comment, this seems to be working correctly for me using Log4J 2.5. 尽管amoe的评论,这似乎对我使用Log4J 2.5正常工作。

On my side, i had to use this code in order to have this working fine (based on previous answers). 在我这边,我不得不使用这个代码,以使此工作正常(基于以前的答案)。

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.AbstractConfiguration;

...

public static void changeLoggerLevel(final String module, final Level level) {
  String moduleRenamed = module.replaceAll("/", ".");
  LoggerContext ctx = (LoggerContext)LogManager.getContext(false);
  AbstractConfiguration configuration = (AbstractConfiguration) ctx
        .getConfiguration();
  if (configuration.getLogger(moduleRenamed) != null) {
    LoggerConfig loggerConfig = configuration.getLoggerConfig(moduleRenamed);
    loggerConfig.setLevel(level);
  } else {
    LoggerConfig loggerConfig = new LoggerConfig(moduleRenamed, level, true);
    configuration.addLogger(moduleRenamed, loggerConfig);
  }
  ctx.updateLoggers(configuration);
}

The problem was with the getLoggerConfig() call; 问题出在getLoggerConfig()调用上; if the module you are trying to give a new level is not yet registered, this method returns the root logger (or any intermediate sub path registered), and thus instead of altering the level for com.mycompany you will alter root or com level. 如果您尝试提供新级别的模块尚未注册,则此方法将返回根记录器(或已注册的任何中间子路径),因此不会更改com.mycompany的级别,而是更改rootcom级别。 That's why you have to add a new LoggerConfig in case the module to alter is not yet registered. 这就是为什么你必须添加一个新的LoggerConfig以防止要修改的模块尚未注册。

Gary Gregory is correct. 加里格雷戈里是对的。

Also the answer to this question is right there on the FAQ page in log4j2's site 此问题的答案就在log4j2网站的FAQ页面上

https://logging.apache.org/log4j/2.x/faq.html#reconfig_level_from_code https://logging.apache.org/log4j/2.x/faq.html#reconfig_level_from_code

Sample Code below: 示例代码如下:

Configurator.setLevel(logger.getName(), Level.INFO);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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