Logger getParent() Method in Java with Examples
Last Updated :
12 Apr, 2023
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
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
{
Logger logger
= Logger.getLogger("com.java.core");
logger = Logger
.getLogger("com.java.core.api");
System.out.println("logger name = "
+ logger
.getParent()
.getName());
}
}
|
Output:
logger name = com.java.core
Program 2:
Java
import java.util.logging.*;
public class GFG {
public static void main(String[] args)
{
Logger logger = Logger.getLogger("com.java");
logger = Logger.getLogger("com.java.core.api.base");
Logger parentLogger = logger.getParent();
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()
Share your thoughts in the comments
Please Login to comment...