Tuesday, October 7, 2025
HomeLanguagesJavaLogger addHandler() method in Java with Examples

Logger addHandler() method in Java with Examples

addHandler() method of a Logger class used to add a log Handler to receive logging messages. A Handler is a component of JVM that takes care of actual logging to the defined output writers like a file, console out etc. one or more Handlers can be added to a Logger. When different types of messages are logged using the Logger, the logs are forwarded to the Handler’s output. By default, Loggers send their output to their parent logger.So we can say Parent Logger is a type of handler for child logger.
Syntax: 
 

public void addHandler(Handler handler)
                throws SecurityException

Parameters: This method accepts one parameter handler which represents a logging Handler.
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 isLoggable() method: 
Program 1: 
 

Java




// Java program to demonstrate
// Logger.addHandler() method
 
import java.util.logging.*;
import java.io.IOException;
 
public class GFG {
 
    private static Logger logger
        = Logger.getLogger(
            GFG.class.getName());
 
    public static void main(String args[])
        throws SecurityException, IOException
    {
 
        // Create a file handler object
        FileHandler handler = new FileHandler("logs.txt");
 
        // Add file handler as
        // handler of logs
        logger.addHandler(handler);
 
        // Log message
        logger.info("This is Info Message ");
        logger.log(Level.WARNING,
                   "Warning Message");
    }
}


Output: 
The output printed on logs.txt file is shown below- 
 

addHandler

addHandler

Program 2: 
 

Java




// Java program to demonstrate
// Logger.addHandler() method
 
import java.util.logging.*;
import java.io.IOException;
 
public class GFG {
 
    private static Logger logger
        = Logger.getLogger(
            GFG.class.getName());
 
    public static void main(String args[])
        throws SecurityException, IOException
    {
 
        // Create a ConsoleHandler object
        ConsoleHandler handler
            = new ConsoleHandler();
 
        // Add console handler as
        // handler of logs
        logger.addHandler(handler);
 
        // Log message
        logger.info("This is Info Message ");
        logger.log(Level.WARNING, "Warning Message");
    }
}


The output printed on console output is shown below- 
 

addHandler(java.util.logging.Handler)

addHandler(java.util.logging.Handler)

References: https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html#addHandler(java.util.logging.Handler)
 

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS