LogRecord setSourceClassName() method in Java with Examples

The setSourceClassName() method of java.util.logging.LogRecord is used to set the name of the class that allegedly issued the logging request. The source class name which is used in logRecord for logging purpose has to be set via this method.

Syntax:

public void setSourceClassName(String sourceClassName)

Parameters: This method accepts sourceClassName as parameter which is the source class name. It can be null also.



Return: This method returns nothing.

Below programs illustrate setSourceClassName() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Source class Name = java.lang.String

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Source class Name = java.util.ArrayList
Now new Source class Name = null

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.