本文介绍了如何在运行时访问已配置的Log4J appender?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在启动时配置appender,然后根据需要从各种记录器中动态添加和删除它。我宁愿让log4j自己配置这个appender,只需在需要时获取它的引用。如果那是不可能的,我将不得不自己实例化appender并坚持下去。

I want to configure an appender at startup and then dynamically add and remove it from various loggers on demand. I'd prefer to have log4j configure this appender itself, and just grab a reference to it when needed. If that's not possible, I'll have to instantiate the appender myself and hold onto it.

推荐答案

Appenders通常被添加到根记录器。这是一些伪代码

Appenders are generally added to the root logger. Here's some pseudocode

// get the root logger and remove the appender we want
Logger logger = Logger.getRootLogger();
Appender appender = logger.getAppender("foo");
logger.removeAppender(appender)

// when we want to add it back...
logger.addAppender(appender);

我很确定你可以在其他记录器上执行此操作,而不是根记录器,尽管我从来没有尝试过。

I'm pretty sure you can do this on other loggers than the root logger as well, though I've never tried that.

这篇关于如何在运行时访问已配置的Log4J appender?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:55