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


Java LogRecord setSourceClassName()用法及代碼示例


java.util.logging.LogRecord的setSourceClassName()方法用於設置據稱發出日誌請求的類的名稱。必須通過此方法設置在logRecord中用於記錄目的的源類名稱。

用法:

public void setSourceClassName(String sourceClassName)

參數:此方法接受sourceClassName作為源類名稱的參數。也可以為null。


返回:此方法不返回任何內容。

以下示例程序旨在說明setSourceClassName()方法:
示例1:

// Java program to illustrate 
// setSourceClassName() method 
  
import java.util.logging.Level; 
import java.util.logging.LogRecord; 
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create LogRecord object 
        LogRecord logRecord 
            = new LogRecord(Level.SEVERE, 
                            "Hello Logger"); 
  
        // set String class as source class 
        logRecord.setSourceClassName( 
            String.class.getName()); 
  
        // print the class name 
        System.out.println( 
            "Source class Name = "
            + logRecord.getSourceClassName()); 
    } 
}
輸出:
Source class Name = java.lang.String

示例2:

// Java program to illustrate 
// setSourceClassName() method 
  
import java.util.logging.Level; 
import java.util.logging.LogRecord; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create LogRecord object 
        LogRecord logRecord 
            = new LogRecord(Level.INFO, 
                            "GFG Logger"); 
  
        // set ArrayList class as source class 
        logRecord.setSourceClassName( 
            ArrayList 
                .class
                .getName()); 
  
        // print the class name 
        System.out.println( 
            "Source class Name = "
            + logRecord.getSourceClassName()); 
  
        // now set null as class name 
        logRecord.setSourceClassName(null); 
  
        // print the class name 
        System.out.println( 
            "Now new Source class Name = "
            + logRecord.getSourceClassName()); 
    } 
}
輸出:
Source class Name = java.util.ArrayList
Now new Source class Name = null

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setSourceClassName(java.lang.String)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LogRecord setSourceClassName() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。