📜  Java中的 Logger throwing() 方法和示例

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

Java中的 Logger throwing() 方法和示例

throwing(String sourceClass, String sourceMethod, Throwable throwed)方法用于记录抛出异常。在许多场景中,方法通过抛出异常而关闭,那么这是一个非常有用的方法来记录方法因抛出异常而终止。日志记录是使用 FINER 级别的日志记录完成的。如果记录器的级别设置为记录 FINER 级别的日志记录,那么给定的参数将存储在日志记录中,并带有一条消息 THROW,该消息将转发到所有已注册的输出处理程序。

句法:

public void throwing(String sourceClass,
                     String sourceMethod,
                     Throwable thrown)

参数:此方法接受三个参数:

  • sourceClass是发出日志请求的类的名称,
  • sourceMethod是方法的名称,并且
  • 结果是被抛出的 Throwable。

返回值:此方法不返回任何内容。

下面的程序说明了 throwing(String sourceClass, String sourceMethod, Object result) 方法:

方案一:

// Java program to demonstrate
// throwing(String, String, Throwable) method
  
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException, IOException
    {
  
        // Create a Logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Create a file handler object
        FileHandler handler
            = new FileHandler("logs.txt");
        handler.setFormatter(new SimpleFormatter());
  
        // Add file handler as
        // handler of logs
        logger.addHandler(handler);
  
        // set Logger level()
        logger.setLevel(Level.FINER);
  
        // set Logger level()
        logger.setLevel(Level.FINER);
  
        // call throwing method with class
        // name =  GFG and method name = main
        // and IO Exception as Thrown object
        logger.throwing(GFG.class.getName(),
                        GFG.class.getMethods()[0].getName(),
                        new IOException());
    }
}

log.txt 上打印的输出如下所示。
输出:

方案二:

// Java program to demonstrate
// throwing(String, String, Throwable) method
  
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
  
public class GFG {
  
    // Create a Logger
    static Logger logger
        = Logger.getLogger(
            GFG.class.getName());
  
    public static void main(String[] args)
        throws SecurityException, IOException
    {
  
        // Create a file handler object
        FileHandler handler
            = new FileHandler("logs.txt");
  
        // Add file handler as
        // handler of logs
        logger.addHandler(handler);
  
        // set Logger level()
        logger.setLevel(Level.FINER);
  
        // set Logger level()
        logger.setLevel(Level.FINER);
  
        // call throwing method with string
        // and ArithmeticException as Thrown object
        logger.throwing(String.class.getName(),
                        String.class.getMethods()[0].getName(),
                        new ArithmeticException());
    }
}

log.txt 上打印的输出如下所示。
输出:

参考: https://docs.oracle.com/javase/10/docs/api/java /util/logging/Logger.html#throwing(java .lang.String, Java Java , Java Java)