开发者

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.


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.


The Logger class has methods to getAllAppenders(), getAppender(), addAppender(), and removeAppender() methods, inherited from the Category class. However, the Category class is deprecated, and on top of that, I've never tried to do this before, but this might be a useful starting point.


I want to do exactly the same thing. I want to configure appenders in log4j.properties and then select some and add the to the rootLogger dynamically at runtime.

I could not figure out how to access the appenders other than via a logger to which they had been attached, so I ended up creating a dummy logger, and attaching the appenders to it so i could retrieve them dynamically. This is not ideal though as any resources used by the appenders (e.g. files) are created upfront even if they are not used.


I would do it this way :

  1. have you appender specified in log4j.properties, but not added to root logger.
  2. at run time, when needed, grab log4j.properties, extract from it the properties you need, instantiate your appender and set its options by reading extracted properties.
  3. activate the appender
  4. Logger.getRootLogger().addAppender(appender);
  5. Kick it off when finished using it - Logger.getRootLogger().removeAppender(..)

Now, if this is your own appender, doing (2) would be easy since you know the meaning of the properties and know what to expect. Otherwise you would probably want to use reflection to instantiate the class and call its property setters before doing (3).


If it is enabling / disabling of Appenders at run time that you want to do then I found another solution (though not very elegant). Using log4j configuration add all the Appenders you would need as you do normally.

At run time when you want to "disable" an appender add a (org.apache.log4j.spi) Filter to it that returns Filter.DENY for every log message. This way no messages make it through for this Appender. When you want to "enable" the Appender back just clear the Filter that you added above.

I tested this and it works well for us (log4j 1.2).


Log4j1

I think the code you are looking for is:

import org.apache.log4j.Appender;

import org.apache.log4j.Logger;

//stuff

    Enumeration enm = Logger.getRootLogger().getAllAppenders();
    Appender appender = null;
    while(enm.hasMoreElements()){
        appender = (Appender)enm.nextElement();
        System.out.println("appender.getName():"+appender.getName());
    }

Log4j2

import org.apache.log4j.Appender;

import org.apache.log4j.Logger;

//stuff

    ((org.apache.logging.log4j.core.Logger) LogManager.getLogger()).getAppenders().values().forEach(a -> {
        System.out.println("appender.getName() " + a.getName());
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜