How to remove appender from logger in log4j2 programmatically?
Asked Answered
E

4

15

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

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.

Thanks.

Endoergic answered 13/1, 2017 at 7:23 Comment(1)
Can you provide a sample configuration file and what do you from that?Dross
Q
12

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();
Quita answered 1/11, 2017 at 12:8 Comment(5)
wont work for me sadly i got 98 old appenders wich should be removed: github.com/stefanwendelmann/JavaLogging/blob/…Yvor
@StefanWendelmann, you are removing appenders from logger config, but you need to do it from rootLogger instead, or your logger directly. Something like config.getLogger(laufId).removeAppender(laufId + "_FILE"))Quita
both wont work, still have a massive amount of Appender + logger in the ctx object config.getLoggerConfig(laufId).removeAppender(laufId+"_FILE"); config.getRootLogger().removeAppender(laufId+"_FILE");Yvor
@AntonBalaniuc Why not call stop() method after removeAppender() ?Conidiophore
clearAppenders() is a protected method and cannot be accessed outside the class.Iqbal
C
4

Developing an idea of Anton (on log4j 2.11.1):

LoggerContext context = LoggerContext.getContext(false);
Configuration configuration = context.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig("loggerName");
if (loggerConfig.getName().equals("loggerName")) {
    loggerConfig.removeAppender("appenderName")
} else {
    throw new RuntimeException("There was no logger " + "loggerName");
}
context.updateLoggers();
Coition answered 24/4, 2019 at 8:1 Comment(2)
Why not call stop() method after removeAppender()?Conidiophore
@gaurav Because it could be used by another logger.Coition
T
2
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(true);
Configuration configuration = loggerContext.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig(_logger.getName());
loggerConfig.getAppenders().forEach((key, value) -> loggerConfig.removeAppender(value.getName()));
Trina answered 4/7, 2020 at 21:24 Comment(0)
R
1

Can you remove an Appender? Yes. Should you? Almost always the answer is no. What if a LoggerConfig, an AsyncAppender or a RoutingAppender is referencing the Appender? You will get an error.

In most cases you really want to use smarter filtering. They are there precisely to reduce or eliminate the number of events being routed to an Appender.

Railroader answered 17/1, 2017 at 4:39 Comment(2)
Good advice. Faced similar issue by writing a cleanup code for logger. Removed appender and logger from configuration. Is there a better way to cleanup loggers in log4j2?Conidiophore
I would like to know that too. I add custom appenders, and I would like to remove them when they are not needed anymore. Otherwise, I would keep accumulating useless appenders.Slowpoke

© 2022 - 2024 — McMap. All rights reserved.