log4j2动态修改日志级别
有时候需要在线上环境排查问题,而一般情况下为了减少日志的输出,设置的日志级别较高,此时就需要一些方法来动态的调低日志级别排查问题。在log4j2中提供了Configurator来修改日志配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @GetMapping(value = "/getLogLevel") @ResponseBody public String getLogLevel(@RequestParam(value = "package") String packageName) { LOGGER.info("getLoglevel packageName is {}", packageName); LoggerContext ctx = (LoggerContext) LogManager.getContext(false); return ctx.getLogger(packageName).getLevel().name(); }
@PostMapping(value = "/setLoglevel") @ResponseBody public String setLoglevel(@RequestParam("loglevel") String logLevel, @RequestParam(value = "package") String packageName) {
LOGGER.info("setLoglevel packageName is {},logLevel is {}", packageName, logLevel); try { Configurator.setLevel(packageName, Level.valueOf(logLevel)); } catch (Exception e) { LOGGER.error("设置失败 setLoglevel packageName is {},logLevel is {}", packageName, logLevel, e); return "fail"; } return "success"; }
|