Skip to content
Related Articles

Related Articles

Improve Article

Logger getParent() Method in Java with Examples

  • Last Updated : 26 Mar, 2019
Geek Week

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:






// 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());
    }
}
Output:
logger name = com.java.core

Program 2:




// 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());
    }
}
Output:
Parent logger name = com.java

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :