Sunday, July 12, 2026
HomeLanguagesJavaLogRecord getSourceMethodName() method in Java with Examples

LogRecord getSourceMethodName() method in Java with Examples

The getSourceMethodName() method of java.lang.reflect.LogRecord is used to get the name of the method that allegedly issued the logging request. The source method which is used in logRecord for logging purpose.

Syntax:

public String getSourceMethodName()

Parameters: This method accepts nothing.

Return: This method returns the source method name.

Below programs illustrate getSourceMethodName() method:
Program 1:




// 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);
    }
}


Output:

Source Method Name = MyFirstSourceMethod

Program 2:




// 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);
    }
}


Output:

Source Method Name = add

References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#getSourceMethodName()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7021 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS