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


Java Logger isLoggable()用法及代碼示例


Logger類的isLoggable()方法用於返回布爾值形式的響應,該響應為查詢提供答案,即如果給定級別的消息實際上是否由該記錄器記錄,則為該記錄。此檢查基於Loggers有效級別,該級別可以從其父級繼承。

用法:

public boolean isLoggable(Level level)

參數:此方法接受一個表示消息日誌記錄級別的參數級別。


返回值:如果當前正在記錄給定的消息級別,則此方法返回true。

以下示例程序旨在說明isLoggable()方法:

示例1:

// Java program to demonstrate 
// Logger.isLoggable() method 
  
import java.util.logging.*; 
  
public class GFG { 
  
    private static Logger logger 
        = Logger.getLogger( 
            GFG.class.getName()); 
  
    public static void main(String args[]) 
    { 
  
        // Check if the Level.INFO 
        // is currently being logged. 
        boolean flag 
            = logger.isLoggable( 
                Level.INFO); 
  
        // Print value 
        System.out.println("The Level.INFO"
                           + " is currently being logged - "
                           + flag); 
    } 
}
輸出:
The Level.INFO is currently being logged - true

示例2:

// Java program to demonstrate 
// Logger.isLoggable() method 
  
import java.util.logging.*; 
  
public class GFG { 
  
    private static Logger logger 
        = Logger.getLogger( 
            GFG.class.getName()); 
  
    public static void main(String args[]) 
    { 
  
        // Check if the Level.OFF 
        // is currently being logged. 
        boolean flag 
            = logger.isLoggable(Level.OFF); 
  
        // Print value 
        System.out.println("The Level.OFF"
                           + " is currently being logged - "
                           + flag); 
    } 
}
輸出:
The Level.OFF is currently being logged - true

參考: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#isLoggable(java.util.logging.Level)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Logger isLoggable() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。