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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。