如何以编程方式从Log4j2中的记录器中删除附加程序? [英] How to remove appender from logger in log4j2 programmatically?

查看:89
本文介绍了如何以编程方式从Log4j2中的记录器中删除附加程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过编程方式从log4j2的记录器中删除附加程序吗?

Is there any way I can remove appender from a logger in log4j2 programmatically ?

网站说:"Log4j 2 API不会公开以任何方式添加,修改或删除附加程序和过滤器的方法,也不会以任何方式操纵配置": https://logging.apache.org/log4j/2.x/manual /configuration.html

The website says "Log4j 2 API does not expose methods to add, modify or remove appenders and filters or manipulate the configuration in any way" : https://logging.apache.org/log4j/2.x/manual/configuration.html

但是,如果有人可以提出一种间接的方法来实现这一目标,我将不胜感激.

But i would really appreciate if someone can suggest an indirect way to achieve this.

谢谢.

推荐答案

有一种方法 LoggerConfig#clearAppenders(),它将删除所有附加程序.假设您有一个名称为CONSOLE的附加器:

There is a method LoggerConfig#removeAppender(String name) which will remove specific appender and LoggerConfig#clearAppenders() which will remove all appenders. Let's say you have an appender with the name CONSOLE:

final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.getRootLogger().removeAppender("CONSOLE");
ctx.updateLoggers();

这篇关于如何以编程方式从Log4j2中的记录器中删除附加程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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