Java Logging: Filters

 

You can set a Filter on a Logger. A Filter can filter out log messages, meaning decide if the message gets logged or not. Filters are represented by the Java interface java.util.logging.Filter

Here is an example of setting a Filter on a Logger:

Filter filter = new MyFilter();

logger1.setFilter(filter);

The Filter interface is defined like this:

public interface Filter {
    public boolean isLoggable(LogRecord record);
}

If the isLoggable() method returns false, the LogRecord is not logged. If the method returns true, theLogRecord is forwarded to the Handler's of the given Logger.

To create a Filter you must implement that interface. Here is a very simple example implementation:

public class MyFilter implements Filter {
    public boolean isLoggable(LogRecord record) {
        return false;
    }
}

This filter rejects all messages. Of course this is not a very useful filter. You would probably inspect the LogRecordand make a decision based on that. You can learn more about the LogRecord in the text on the LogRecord, and in the JavaDoc too.

For a discussion of how Filter's work within the Logger hierarchy, see the text on the Logger hierarchy.

 

转载于:https://www.cnblogs.com/hephec/p/4579626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值