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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。