当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Logger getFilter()用法及代码示例


Logger类的getFilter()方法用于获取此Logger实例的当前过滤器。筛选器可用于筛选出日志消息。我们可以说过滤器决定消息是否被记录。过滤器由Java接口java.util.logging.Filter表示

用法:

public Filter getFilter()

参数:此方法接受不接受任何参数。


返回值:此方法返回此Logger的当前过滤器。

以下示例程序旨在说明getFilter()方法:
示例1:

// Java program to demonstrate 
// Logger.getFilter() method 
  
import java.util.logging.*; 
import java.io.IOException; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws SecurityException, IOException 
    { 
  
        // create a logger 
        Logger logger = Logger.getLogger("com.core"); 
  
        // set a new filter 
        logger.setFilter(new Filter() { 
            @Override
            public boolean isLoggable(LogRecord record) 
            { 
                return true; 
            } 
        }); 
  
        // get Filter 
        Filter filter = logger.getFilter(); 
  
        // check filter is null or not by printing 
        System.out.println("Filter = " + filter); 
    } 
}

输出:
eclipse IDE上打印的输出如下所示-

示例2:

// Java program to demonstrate 
// Logger.getFilter() method 
  
import java.util.logging.*; 
import java.io.IOException; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws SecurityException, IOException 
    { 
  
        // create a logger 
        Logger logger 
            = Logger.getLogger("com.javacode.core"); 
  
        // set a new filter 
        logger.setFilter(new MyFilter()); 
  
        // get Filter 
        Filter filter = logger.getFilter(); 
  
        // check filter is null or not by printing 
        System.out.println("Filter = " + filter); 
    } 
} 
class MyFilter implements Filter { 
    public boolean isLoggable(LogRecord record) 
    { 
        return false; 
    } 
}

输出:
eclipse IDE上打印的输出如下所示-

参考: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#getFilter()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Logger getFilter() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。