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


Java Logger throwing()用法及代码示例


throwing(String sourceClass,String sourceMethod,Throwable thrown)方法用于记录引发异常的日志。在许多情况下,方法是通过引发异常关闭的,这对于记录某个方法因引发异常而终止是非常有用的方法。使用FINER级别的日志记录完成日志记录。如果记录器的级别设置为记录FINER级别的记录,则给定的参数将与消息THROW一起存储在日志记录中,该消息将转发到所有已注册的输出处理程序。

用法:

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

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


  • sourceClass是发出日志记录请求的类的名称,
  • sourceMethod是方法的名称,
  • result是被抛出的Throwable。

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

以下示例程序旨在说明throwing(String sourceClass,String sourceMethod,Object result)方法:

示例1:

// 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上的输出如下所示。
输出:

示例2:

// 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.lang.String, java.lang.Throwable)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Logger throwing() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。