Friday, February 6, 2026
HomeLanguagesJavaLogger setParent() method in Java with Examples

Logger setParent() method in Java with Examples

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-

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32489 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12073 POSTS0 COMMENTS
Shaida Kate Naidoo
6995 POSTS0 COMMENTS
Ted Musemwa
7236 POSTS0 COMMENTS
Thapelo Manthata
6946 POSTS0 COMMENTS
Umr Jansen
6930 POSTS0 COMMENTS