Logger getParent() Method in Java with Examples

getParent() method of a Logger class is used to get the parent of this Logger.This method returns the nearest extant parent in the namespace.if there is a Logger called “com.javac.core.api”, and a Logger called “com.javac” has been created but no logger “com.javac.core” exists, then a call of getParent on the Logger “com.javac.core.api” will return the Logger “com.javac”. The result will be null if we apply getParent() method on root Logger in the namespace.

Syntax:

public Logger getParent()

Parameters: This method accepts nothing.



Return value: This method return nearest existing parent Logger.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

logger name = com.java.core

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Parent logger name = com.java

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



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.