📜  Java中的 Logger isLoggable() 方法及示例

📅  最后修改于: 2022-05-13 01:55:08.845000             🧑  作者: Mango

Java中的 Logger isLoggable() 方法及示例

Logger类的isLoggable()方法用于返回布尔值的响应,该响应提供了对查询的答案,即给定级别的消息是否实际由该记录器记录。此检查基于 Loggers 有效级别,该级别可能继承自其父级。

句法:

public boolean isLoggable(Level level)

参数:此方法接受一个参数级别,表示消息记录级别。

返回值:如果当前正在记录给定的消息级别,则此方法返回 true。

下面的程序说明了 isLoggable() 方法:

方案一:

// 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

方案二:

// 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: Java Java.util.logging.Level)