Wednesday, July 3, 2024
HomeLanguagesJavaLogger removeHandler() method in Java with Examples

Logger removeHandler() method in Java with Examples

removeHandler() method of a Logger class is used to remove a log Handler from Logger. A Handler is a component of JVM that takes care of actual logging to the defined output writers like a file, console out etc. It returns silently if the given Handler is not found or is null.
Syntax: 
 

public void removeHandler(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 removeHandler() method: 
Program 1: 
 

Java




// Java program to demonstrate
// Logger.removeHandler() 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
    {
 
        FileHandler filehandler
            = new FileHandler("logs.txt");
 
        // Add file handler as
        // handler of logs
        logger.addHandler(filehandler);
 
        // Log message
        logger.info("This is Info Message ");
 
        // Remove file handler.
        logger.removeHandler(filehandler);
 
        logger.info("This message will "
                    + "not print on filehandler");
    }
}


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

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 ");
 
        // Remove consolehandler
        logger.removeHandler(handler);
 
        // After removing logs print message
        logger.info("Handler removed");
    }
}


Output: 
output printed on console output is shown below- 
 

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

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