Logger setUseParentHandlers() method in Java with Examples

setUseParentHandlers() method of a Logger class used to set the configuration which defines whether or not this logger should send its output to its parent Logger. if we want to send the output to its parent Logger then we have to set the parameter to this method equal to true. This means that any log records will also be written to the parent’s Handlers, and potentially to its parent, recursively up the namespace.

Syntax:

public void setUseParentHandlers(boolean useParentHandlers)

Parameters: This method accepts one parameter useParentHandlers which represents true if output is to be sent to the logger’s parent.



Return value: This method returns nothing.

Exception: This method throws SecurityException if a security manager exists, this logger is not anonymous, and the caller does not have LoggingPermission(“control”).

Below programs illustrate the setUseParentHandlers() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setUseParentHandlers() method
  
import java.util.logging.Logger;
  
public class GFG {
  
    private static Logger logger
        = Logger.getLogger(
            GFG.class
                .getPackage()
                .getName());
  
    public static void main(String args[])
    {
  
        // Set that this logger will
        // sent logs to its parent logger.
        logger.setUseParentHandlers(true);
  
        // Log the flag value
        logger.info("output sent to the"
                    + " logger's parent - "
                    + logger.getUseParentHandlers());
    }
}

chevron_right


Output:
The output printed on console of Eclipse is shown below-

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Logger.setUseParentHandlers() method
  
import java.util.logging.Logger;
  
public class GFG {
  
    private static Logger logger
        = Logger.getLogger(
            GFG.class
                .getPackage()
                .getName());
  
    public static void main(String args[])
    {
  
        // Set that this logger will not
        // send logs to its parent logger.
        logger.setUseParentHandlers(false);
  
        // Print the flag value
        System.out.println("output sent to the"
                           + " logger's parent - "
                           + logger.getUseParentHandlers());
    }
}

chevron_right


Output:
The output printed on console output is shown below-

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.