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?
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.
精彩评论