Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

java - How can I access the configured Log4J appenders at runtime?

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.

question from:https://stackoverflow.com/questions/1909871/how-can-i-access-the-configured-log4j-appenders-at-runtime

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...