BestPractices
BestPractices

Reputation: 12876

Log4j: How filter out messages from a specific class?

I want to configure my log4j logging to not log messages that are 1) coming from a specific class and 2) of a specific severity i.e. WARN.

Can someone provide sample configuration/code on how to do this?

Upvotes: 2

Views: 2043

Answers (1)

Dmitry
Dmitry

Reputation: 2993

The information below solves the problem for standart java.util.logging package:

For filtering classes you should implement Filter inteface and use it by calling setFilter method of Logger class:

public class ClassFilter implements Filter {
  public boolean isLoggable(LogRecord lr) {
    return (lr.getSourceClassName() == "SpecificClassName");
  }
}
...
Logger l = Logger.getLogger("mypackage.log");
l.setFilter(new ClassFilter());

For filtering severity use setLevel() method of Logger class.

Upvotes: 2

Related Questions