📌  相关文章
📜  Java中的 LogRecord getSourceMethodName() 方法及示例

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

Java中的 LogRecord getSourceMethodName() 方法及示例

Java.lang.reflect.LogRecordgetSourceMethodName()方法用于获取据称发出日志请求的方法的名称。在 logRecord 中用于记录目的的源方法。

句法:

public String getSourceMethodName()

参数:此方法不接受任何内容。

返回:此方法返回源方法名称

下面的程序说明了 getSourceMethodName() 方法:
方案一:

// Java program to illustrate
// getSourceMethodName() 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.parse("800"),
            "Hi Logger");
        logRecord.setSourceMethodName(
            "MyFirstSourceMethod");
  
        // get the source method name
        String sourceMethod
            = logRecord.getSourceMethodName();
  
        // print the method name
        System.out.println(
            "Source Method Name = "
            + sourceMethod);
    }
}
输出:
Source Method Name = MyFirstSourceMethod

方案二:

// Java program to illustrate
// getSourceMethodName() 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.parse("600"),
            "GFG Logger");
        logRecord.setSourceMethodName(
            ArrayList
                .class
                .getMethods()[0]
                .getName());
  
        // get the source method name
        String sourceMethod
            = logRecord.getSourceMethodName();
  
        // print the method name
        System.out.println(
            "Source Method Name = "
            + sourceMethod);
    }
}
输出:
Source Method Name = add

参考资料: https: Java/util/logging/LogRecord.html#getSourceMethodName()