Java Util Logging ErrorManager example

In this post under java logging, I will explain the purpose of ErrorManager and how we can use it.

Whenever we write the below code“Hello my name is Sumanth1”);

We are requesting the Handler to log the message to a destination. What if an error happens at the Handler level when an attempt is made to log the message.

To handle those situations, an Handler is given an instance of ErrorManager, which handles the error happening at the Handler level.

By default an instance of ErrorManager is given to all the handlers. The purpose of it is to log error message in the console.

We can change the default implemenatation by setting our own custom ErrorManager to these handlers.

For our example I have created a custom ErrorManager which logs error messages to a file error.log as shown below


1  package logging;
3  import;
4  import java.util.logging.ErrorManager;
5  import java.util.logging.FileHandler;
6  import java.util.logging.Logger;
8  public class CustomErrorManager extends ErrorManager{
9   @Override
10  public synchronized void error(String msg, Exception ex, int code) {
11      Logger logger = Logger.getLogger("logger1");
12      FileHandler fileHandler = null;
13      try {
14          fileHandler = new FileHandler("error.log");
15          logger.addHandler(fileHandler);
16 + ":" + msg + ":" + code);
17          logger.removeHandler(fileHandler);
18      } catch(IOException excep) {
19          excep.printStackTrace();
20      } finally {
21          fileHandler.close();
22      }
23  }
24 }

As shown above I have created a class CustomErrorManager which extends ErrorManager class and overrides the error method.

For our example I have created a custom handler ‘MyDatabaseHandler’ to which the custom error manager ‘CustomErrorManager’ is assigned as shown below


1  package logging;
3  import java.util.logging.LogRecord;
4  import java.util.logging.StreamHandler;
6  public class MyDatabaseHandler extends StreamHandler {
7   @Override
8   public synchronized void publish(LogRecord record) {
9       try {
10          throw new NullPointerException("Null Error");
11      } catch(NullPointerException excep) {
12          reportError("Custom Error", excep, 0);
13      }
14  }
15 }

As shown in the above code, to simulate an error thrown by Handler, I have overridden the publish method to throw NullPointerException whenever we try to log any message through MyDatabaseHandler. We catch the error in the catch block and call reportError method (this method is available in the java.util.logging.Handler). This method calls our CustomErrorManager’s error method to handle the exception.

In the main method as shown below I will set the an instance of CustomErrorManager to an instance of MyDatabaseHandler as shown below.

Main Class

1  package logging;
3  import;
4  import java.util.logging.Logger;
6  public class LoggingDemo8 {
7   public static void main(String[] args) throws IOException {
8       Logger logger = Logger.getLogger("logger");
9       MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler();
10      try {
11          logger.addHandler(myDatabaseHandler);
12          myDatabaseHandler.setErrorManager(new CustomErrorManager());
13"Hello my name is Sumanth2");
14          logger.removeHandler(myDatabaseHandler);
15      } finally {
16          if(myDatabaseHandler != null) {
17              myDatabaseHandler.close();
18          }
19      }
20  }
21 }

At line 12, I set an instance of CustomErrorManager to an instance of MyDatabaseHandler.

From now on any error at the Handler’s level, will handled by CustomErrorManager, which prints the error information to a file.


<?xml version=”1.0″ encoding=”windows-1252″ standalone=”no”?>
<!DOCTYPE log SYSTEM “logger.dtd”>
<message>Null Error:Custom Error:0</message>

Check these products on Amazon

Leave a Reply