0
votes

I'm trying to reach a configuration in log4j which I'm not sure that is supported...

here's the scenario:

I've defined one logger for package "com.abc" and another logger for package "com" (both with additivity=true).

this practically means that any logging in a class com.abc.myClass will be logged to both loggers...

my goal is to be able to define the 2nd logger (parent logger) to log any message that came directly to that logger e.g. from class com.myClass while logging only ERROR level messages that came through additivity functionality and were already logged in descendant loggers...

if I set the logger for "com" to ERROR I will only see Error level messages while I'm willing to see INFO level messages of the ones which written directly to this log...

is that possible ? something like setting additivity logging level or so...

thanks, GB.

1

1 Answers

1
votes

Create your own Log4J Filter.

Attach this filter to the parent appender and only for "com.abc" events filter them if their level is not ERROR.

A quick example:

public class MyCustomFilter extends Filter
{
  @Override
  public int decide(final LoggingEvent event) {
    if (event. getFQNOfLoggerClass().startsWith("com.abc.") && !event.getLevel().isGreaterOrEqual(Level.ERROR) {
      return DENY;
    }
    return NEUTRAL;
  }
}

Naturally you have to implement a better condition that fits all your needs.