LogRecord setSourceMethodName() method in Java with Examples
Last Updated :
18 Oct, 2019
The setSourceMethodName() method of java.util.logging.LogRecord is used to set the name of the method that allegedly issued the logging request. The source method name which is used in logRecord for logging purpose has to be set via this method.
Syntax:
public void setSourceMethodName(String sourceMethodName)
Parameters: This method accepts sourceMethodName as a parameter which is the source method name. It can be null also.
Return: This method returns nothing.
Below programs illustrate setSourceMethodName() method:
Program 1:
import java.util.logging.Level;
import java.util.logging.LogRecord;
public class GFG {
public static void main(String[] args)
{
LogRecord logRecord
= new LogRecord(Level.SEVERE,
"Hello Logger" );
logRecord
.setSourceMethodName(
"MyFirstSourceMethod" );
System.out.println(
"Source method Name = "
+ logRecord.getSourceMethodName());
}
}
|
Output:
Source method Name = MyFirstSourceMethod
Program 2:
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
LogRecord logRecord
= new LogRecord(Level.INFO,
"GFG Logger" );
logRecord.setSourceMethodName(
ArrayList
. class
.getMethods()[ 0 ]
.getName());
System.out.println(
"Source method Name = "
+ logRecord.getSourceMethodName());
logRecord.setSourceMethodName( null );
System.out.println(
"Now new Source method Name = "
+ logRecord.getSourceMethodName());
}
}
|
Output:
Source method Name = add
Now new Source method Name = null
References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setSourceMethodName(java.lang.String)
Share your thoughts in the comments
Please Login to comment...