Reporting Application Errors by Email
by Sean C. Sullivan09/29/2004
It is common practice for server-side applications to log messages to files on the server's file system. These logs are a vital source of information for system administrators and the application development team. If a user calls to report an error in the application, the developers can review the log files to determine what is happening in the application.
When your team has to support a mission-critical application, you
can't afford to wait for users to tell you that the application is having
problems. You need to be able to detect problems as soon as they occur. A
simple and inexpensive solution is to implement an email notification system.
The notification system will generate an email message whenever the application
detects an error. If you are already using a standard logging framework like
log4j,
you can easily configure the logging system to broadcast error messages
via email. In this article, we describe how to set up email notification for the
two most widely used Java logging frameworks: log4j and
java.util.logging.
log4j and java.util.logging
Let's start by reviewing the features and capabilities of
log4j and java.util.logging. These logging frameworks provide the ability to
categorize log messages, control over which messages will be logged,
configurable message formatting, and configurable output destinations. The following table
summarizes the core features of each logging framework.
log4j |
java.util.logging |
|
|---|---|---|
| Manager class | org.apache.log4j.LogManager |
java.util.logging.LogManager |
| Logger objects | org.apache.log4j.Logger |
java.util.logging.Logger |
| Named loggers | Supported | Supported |
| Logging levels | Levels are declared in the
|
Levels are declared in the
|
| Log event object | org.apache.log4j.spi.LoggingEvent |
java.util.logging.LogRecord |
| Output destinations | Appender classes. These classes implement the org.apache.log4j.Appender interface. |
Handler classes. These classes extend java.util.logging.Handler. |
| Output message filtering | Filter classes. These classes extend org.apache.log4j.spi.Filter. |
Filter classes. These classes must implement the java.util.logging.Filter interface. |
| Output message formatting | Layout classes extend org.apache.log4j.Layout. |
Formatter classes extend java.util.logging.Formatter. |
| Programmatic configuration | Supported | Supported |
| Property file configuration | Supported | Supported |
| XML configuration | Supported. The XML configuration file must comply with log4j.dtd. | Not supported |
| Email notification | SMTPAppender class |
SMTPHandler class (third-party extension) |
If you are using java.util.logging, your application
primarily uses the java.util.logging.Logger class to create log messages. This
simple program demonstrates how to create a FINE-level message and a SEVERE-level message with the java.util.logging.Logger class:
import java.util.logging.*;
public class JDKLoggingExample
{
private static final Logger log =
Logger.getLogger(JDKLoggingExample.class.getName());
public static void main(String[] args)
{
try
{
log.fine("This is a fine message.");
int i = Integer.parseInt("Hello world");
}
catch (java.lang.Exception ex)
{
log.log(Level.SEVERE, "Caught an exception", ex);
}
}
}
log4j has a similar approach. An application uses log4j's org.apache.log4j.Logger class to produce log
messages. The following program demonstrates how to produce a DEBUG-level
message and an ERROR-level message via log4j's Logger class.
import org.apache.log4j.*;
public class Log4jExample
{
private static final Logger log =
Logger.getLogger(Log4jExample.class);
public static void main(String[] args)
{
try
{
log.debug("This is a debug message.");
int i = Integer.parseInt("Hello world");
}
catch (java.lang.Exception ex)
{
log.error("Caught an exception", ex);
}
}
}
Appenders and Handlers
Both log4j and java.util.logging are capable of dispatching log messages to
multiple destinations. log4j accomplishes this task by routing log messages
through appender objects. Similarly, java.util.logging routes messages
through handler objects. log4j users will typically configure a list of
appenders in the log4j.xml file; java.util.logging users will typically
configure a list of handlers in the logging.properties file.
log4j provides the SMTPAppender class so that applications can broadcast errors to an SMTP mail server. java.util.logging applications accomplish the same task with the SMTPHandler
class.
Let's take a closer look at the SMTPAppender and the SMTPHandler.
log4j's SMTPAppender
The log4j JAR includes the org.apache.log4j.net.SMTPAppender class. With
proper configuration, log4j will route error messages to the SMTPAppender. The
SMTPAppender is then responsible for constructing an email message. The
appender employs the JavaMail API to send the email message to an SMTP server.
Figure 1 illustrates the relationship between the log4j Logger,
the SMTPAppender, and the SMTP server.

Figure 1. log4j Logger and SMTPAppender
The SMTPAppender makes it easy to broadcast application errors to your
entire support team. Figure 2 shows an example email message from the SMTPAppender class.

Figure 2. An email message from the SMTPAppender
Next, we'll discuss how to configure log4j and how to enable the SMTPAppender.
Pages: 1, 2 |