简体   繁体   中英

How to programmatically remove an Appender from Log4j2 configuration/ Any other Log4j2 container?

In Log4j2.x one can add an Appender to a Logger Configuration as below:

Appender appender =//get an appender
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ctx.getConfiguration().addAppender(appender);

Now, how to remove this appender from Configuration? I can't find a remove method in Configuration.

In Log4j1.x one can remove an appender from AppenderAttachable as below:

AppenderAttachable appenderContainer = //get appenderContainer 
String appenderName = // get appenderName 
appenderContainer.removeAppender( appenderName );

Here, I don't want to add an Appender to a Logger. Just need to add and remove it from a Configuration OR Any other Log4j2 container.

I don't think this is possible in Log4j2.x as there is No removeAppender methods other than removing appender from a Logger/LoggerConfig.

However, this can be done through AbstractConfiguration as said by @rgoers .

[ EDIT ] One can make use of AbstractConfiguration class which implements Configuration as below:

import org.apache.logging.log4j.core.config.AbstractConfiguration; 

private static LoggerContext ctx = (LoggerContext) LogManager.getContext(false);

/**
 * Remove an appender from configuration.
 * @param appenderName
 */
public static void removeAppender(String appenderName) {
    appenderName = //Trim the String;
    if (appenderName == null)
        throw new NullPointerException("CUSTOM_ERROR_MESSAGE");

    AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
    config.removeAppender(appenderName);
}

/**
 * Remove all AsyncAppenders from configuration for a given AsyncAppender.
 * @param asyncAppender
 */
public static void removeAllAppenders(final AsyncAppender asyncAppender) {
    final String appenderNames[] = asyncAppender.getAppenderRefStrings();
    if (null != appenderNames) {
        for (String appenderName : appenderNames) {
            removeAppender(appenderName);
        }
    }
}

Actually, Rohit Gaikwad's answer is only partially correct. While the Configuration interface does not provide a removeAppender method, AbstractConfiguration - the class that all Log4j2 provided Configurations extend - does provide one. This method will remove the Appender from any LoggerConfigs that reference it, removes it from the Configuration, and then stops the Appender.

Although the removeAppender is synchronized, which guarantees the data structures won't be corrupted, it does not guarantee you won't have problems. Logging will continue while the appender is being removed, which means events could be routed to the appender while it is being removed and the events being processed could try to be processed when the appender is in the stopped state, which will cause logging of those events to fail. This is the primary reason why it is not part of the interface - to let you know that the Log4j team considers this to be an internal method that we primarily use in our tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM