Logger setParent() method in Java with Examples

setParent() method of a Logger class used to set the parent Logger of this current Logger.The parent Logger we want to set is passed as a parameter. LogManager use this method to update a Logger when the namespace changes.

Syntax:

public void setParent(Logger parent)

Parameters: This method accepts one parameter parent which represents the new parent logger.



Return value: This method returns nothing.

Exception: This method throws SecurityException if a security manager exists and if the caller does not have LoggingPermission(“control”).

Below programs illustrate the setParent() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setParent() method
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a logger1 using getLogger()
        Logger logger1
            = Logger.getLogger("com.java.core");
  
        // Assign other package to logger
        Logger logger2
            = Logger
                  .getLogger("com.java.core.api");
  
        // Set logger1 as parent to logger2
        logger2.setParent(logger1);
  
        // Print parent name
        System.out.println("logger2 parent name = "
                           + logger2
                                 .getParent()
                                 .getName());
    }
}

chevron_right


Output:
The output printed on console of Eclipse is shown below-

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setParent() method
  
import java.util.logging.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a logger1 using getLogger()
        Logger logger1
            = Logger.getLogger(GFG.class.getName());
  
        // Assign other package to logger
        Logger logger2
            = Logger
                  .getLogger(String.class.getName());
  
        // Set logger2 as parent to logger1
        logger1.setParent(logger2);
  
        // Print parent name
        System.out.println("logger1 parent name = "
                           + logger1
                                 .getParent()
                                 .getName());
    }
}

chevron_right


Output:
The output printed on console output is shown below-

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#setParent(java.util.logging.Logger)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.