0

I have 2 appenders - A, B. By default, I want all messages go to B. If message has level >= ERROR or it's from com.mycomp.* logger I want it to go to A instead.

I'm trying to figure out if this configuration is possible in log4j 1.2. So far seems like it's not.

Edit: to clarify, here's exactly same question about Logback logback: Two appenders, multiple loggers, different levels

Community
  • 1
  • 1
ptkvsk
  • 2,096
  • 1
  • 25
  • 47
  • Yes. You are right, by setting same package to different error-levels/appenders overrides the other one. So I would suggest to add appenders programmatically. – rajuGT Nov 09 '15 at 22:37

1 Answers1

0

Can be done using filter:

public class MyLogFilter extends Filter {
    @Override
    public int decide(LoggingEvent loggingEvent) {
        if (!loggingEvent.getLevel().isGreaterOrEqual(Level.ERROR) &&
                !loggingEvent.getLoggerName().startsWith("com.mycomp")) {
            return DENY;
        }

        return NEUTRAL;
    }
}

and configuration like

log4j.rootLogger=(INFO, A, B);
log4j.logger.com.mycomp=(INFO, A);
log4j.appenders.A.filter.a=com.mycomp.MyLogFilter;

Unfortunately, you can only specify filters in log4j.xml and not in log4j.properties.

Community
  • 1
  • 1
ptkvsk
  • 2,096
  • 1
  • 25
  • 47