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 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 Messsage "); // 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 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 Messsage "); // Remove consolehandler logger.removeHandler(handler); // After removing logs print message logger.info("Handler removed"); } } |
Output:
output printed on console output is shown below-

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Logger log() Method in Java with Examples
- Logger setParent() method in Java with Examples
- Logger setResourceBundle() method in Java with Examples
- Logger addHandler() method in Java with Examples
- Logger setUseParentHandlers() method in Java with Examples
- Logger getHandler() Method in Java with Examples
- Logger getFilter() Method in Java with Examples
- Logger setLevel() method in Java with Examples
- Logger setFilter() method in Java with Examples
- Logger getName() Method in Java with Examples
- Logger getAnonymousLogger() Method in Java with Examples
- Logger getResourceBundle() Method in Java with Examples
- Logger getGlobal() Method in Java with Examples
- Logger getParent() Method in Java with Examples
- Logger exiting() method in Java with Examples
- Logger getResourceBundleName() method in Java with Examples
- Logger getUseParentHandlers() Method in Java with Examples
- Logger isLoggable() method in Java with Examples
- Logger entering() method in Java with Examples
- Logger getLogger() Method in Java with Examples
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.

