Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the best way to notify admin about new exceptions of the Java application?

My question is that whats the best way to keep track of the exceptions for the administrator of the application. (Notify administrator of the thrown exceptions for maintenance purposes).

To users of the system, I believe should catch the exceptions and show an appropriate error message. To admin of the system, I suppose, best method is to have a messaging system to send details of each exception as a message to the receiver. Once receiver received a new error message persists it in the database or pings the admin an email with details of the exception.

try{
  ....
}
catch(Exception e){
   //what to do here? how to notify admin?
}
like image 230
Jack Avatar asked May 14 '14 05:05

Jack


People also ask

How do you handle exceptions in Java application?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What are the two ways to handle exceptions in Java?

Java provides two different options to handle an exception. You can either use the try-catch-finally approach to handle all kinds of exceptions. Or you can use the try-with-resource approach which allows an easier cleanup process for resources.

How do you send an exception in Java?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.


2 Answers

I'd suggest using log4j, configured with an SMTPAppender listening to fatal logs. Then, just log a fatal level message (containing any useful information you can get) for any unhandled exception reaching your global try/catch block.

See also : What is the proper way to configure SMTPAppender in log4j?

like image 53
KeatsPeeks Avatar answered Sep 24 '22 07:09

KeatsPeeks


Enterprise solution:

Use SL4J and save all messages to your logs.

Use MDC to add tags to your log messages. Have these tags describe who should be notified and the nature of the error:

2014-05-24 [SystemCAD][NOTIFY=ADMIN], [ACCOUNTID=123], [SEVERITY=SEVERE], [MESSAGE="Cannot contact Google.com"]  
2014-05-24 [SystemCAD][NOTIFY=USER], [ACCOUNTID=123], [SEVERITY=SEVERE], [MESSAGE="Could not save document to Google. Support has been notified."]  

Get Splunk or some product similar to index all your logs for easy searching and to create events which can be used to notify your admins. Use PagerDutty to notify your admins and create escalation, avoid duplicates, create triggers, etc.

like image 38
Alexandre Santos Avatar answered Sep 22 '22 07:09

Alexandre Santos