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.
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:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
The output printed on logs.txt file is shown below-
The output printed on console output is shown below-