Wednesday, July 3, 2024
HomeLanguagesJavaLogger getHandler() Method in Java with Examples

Logger getHandler() Method in Java with Examples

The getHandlers() method of the Logger class is used to get the Handlers linked with this logger. Handler is used to taking care of the actual logging. one or more Handler can be added to a Logger. When messages are logged via the Logger, the messages are forwarded to the Handler. This method is helpful for getting an array of all registered Handlers.

Syntax:

public Handler[] getHandlers()

Parameters: This method accepts nothing.

Return value: This method return an array of all registered Handlers.

Below programs illustrate the getHandlers() method:
Program 1:




// Java program to demonstrate
// Logger.getHandler() method
  
import java.util.logging.*;
import java.io.IOException;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException, IOException
    {
  
        // Create a logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Log some logs
        logger.info("This is message 1");
        logger.info("This is message 2");
        logger.info("This is message 3");
  
        // Get handler details using getHandler
        Handler[] handlers = logger.getHandlers();
  
        // Log handler length
        logger.info("length of Handler "
                    + handlers.length);
    }
}


Output:
The output printed on eclipse IDE is shown below-

Program 2:




// Java program to demonstrate
// Logger.getHandler() method
  
import java.util.logging.*;
import java.io.IOException;
  
public class GFG {
  
    public static void main(String[] args)
        throws SecurityException, IOException
    {
  
        // Create a logger
        Logger logger
            = Logger.getLogger(
                GFG.class.getName());
  
        // Set a console Handler
        logger.addHandler(new ConsoleHandler());
  
        // Get handler details using getHandler
        Handler[] handlers = logger.getHandlers();
  
        // Print handler details
        for (int i = 0; i < handlers.length; i++) {
            System.out.println("Handler details: "
                               + handlers[i].toString());
        }
    }
}


Output:
The output printed on eclipse IDE is shown below-

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments