Wednesday, July 02, 2008

Send Email Alerts Using Log4j [Howto]

It's really easy to add error alerting to your java application. If your application logs all errors using log4j and you want these errors emailed out to a support team or to yourself, then all you have to do is add another appender to your log4j.properties file.

Let's say you have the following class which logs an error message: And your log4j properties file is: Now add a new MAIL appender, so that your properties file looks like this: Don't forget to place mail.jar and activation.jar on your classpath and then run the application.

Check your inbox and voila, you have an alert without making any source code changes!

NB: By default, an email message will be sent when an ERROR or higher severity message is appended. If you want to email messages with levels less than ERROR (e.g. INFO) then you currently have to configure your own implementation of the TriggeringEventEvaluator. Setting log4j.appender.MAIL.Threshold=INFO will not work.

Further Reading:
SMTPAppender javadoc
SmtpAppender Members and Properties

7 comments:

  1. hi!
    I used the code you have posted to alert when an error occurs, also added activation and mail.jar to my classpath. I am using jdk 1.6_03JDK, and so when I use eclipse to run, it works like a charm. However when I generate a JAR using fat jar (a plugin for eclipse for generating jars), I get wierd errors.. posted below..
    any help here..
    Exception in thread "Thread-1" java.lang.NoSuchMethodError: com.sun.mail.util.So
    cketFetcher.getSocket(Ljava/lang/String;ILjava/util/Properties;Ljava/lang/String
    ;Z)Ljava/net/Socket;
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:41
    2)
    at javax.mail.Service.connect(Service.java:233)
    at javax.mail.Service.connect(Service.java:134)
    at javax.mail.Service.connect(Service.java:86)
    at javax.mail.Transport.send0(Transport.java:162)
    at javax.mail.Transport.send(Transport.java:80)
    at org.apache.log4j.net.SMTPAppender.sendBuffer(Unknown Source)
    at org.apache.log4j.net.SMTPAppender.append(Unknown Source)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)

    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders
    (AppenderAttachableImpl.java:57)
    at org.apache.log4j.Category.callAppenders(Category.java:187)
    at org.apache.log4j.Category.forcedLog(Category.java:372)
    at org.apache.log4j.Category.error(Category.java:286)
    at FileBasedLoggingDemo.run(FileBasedLoggingDemo.java:32)
    at java.lang.Thread.run(Unknown Source)

    ReplyDelete
  2. Anonymous12:56 AM

    Is there a way to disable sending email message when an error occurs.

    ReplyDelete
  3. Setting log4j.appender.MAIL.Threshold=ERROR will prevent sending emails for debug statements when you have logging cranked up to DEBUG level.

    ReplyDelete
  4. This type of lace wig looks very realistic and is made from sophisticated lace material having the original look. Not just full lace wigs,lace front wigs,full lace wigs gives a natural character but lace wigs and lace front wigs also render a fashionable look to the face. The lace wigs in this wig usually appears as if it is growing out of the scalp and equally distributed. Some manufacturers now are trying to make their lace front human hair wigs inexpensive so that ladies who really need it can afford to buy it.

    ReplyDelete
  5. Anonymous4:37 PM

    In a multi-server environment situation, is there a way that log4j logs the name of the server?

    ReplyDelete
  6. I want to add a throttling mechanism for sending emails that will be added by extending the “TriggerEvaluator” class and adding a timing mechanism for sending emails related to the same trigger / error.

    How and where to do this?

    ReplyDelete
  7. Hi Fahd,

    I followed the code that u posted. But I'm not able to achieve it.. I am not getting any email when it throws log.error().

    ReplyDelete