當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。