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

logger.info(“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

CustomErrorManager


1  package logging;
2  
3  import java.io.IOException;
4  import java.util.logging.ErrorManager;
5  import java.util.logging.FileHandler;
6  import java.util.logging.Logger;
7  
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          logger.info(ex.getMessage() + ":" + 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

MyDatabaseHandler


1  package logging;
2  
3  import java.util.logging.LogRecord;
4  import java.util.logging.StreamHandler;
5  
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;
2  
3  import java.io.IOException;
4  import java.util.logging.Logger;
5  
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          logger.info("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.

Output

<?xml version=”1.0″ encoding=”windows-1252″ standalone=”no”?>
<!DOCTYPE log SYSTEM “logger.dtd”>
<log>
<record>
<date>2019-03-16T13:01:01</date>
<millis>1552721461738</millis>
<sequence>1</sequence>
<logger>logger1</logger>
<level>INFO</level>
<class>logging.CustomErrorManager</class>
<method>error</method>
<thread>1</thread>
<message>Null Error:Custom Error:0</message>
</record>
</log>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s