Monday, November 18, 2024
Google search engine
HomeLanguagesJavaLogger getParent() Method in Java with Examples

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 getParent() method: 

Program 1: 

Java




// 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




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

RELATED ARTICLES

Most Popular

Recent Comments