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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。