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:
// 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()); } } |
Output:
The output printed on console of Eclipse is shown below-
Program 2:
// 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()); } } |
Output:
The output printed on console output is shown below-