August 18, 2005

Log4j SMTPAppender

Today I had to get some messages sent to an administrator from within the new SSO system. The old way of doing it would be to put in some emailing code to send the email wherever it was needed.

However….I'm lazy. We use log4j for logging without our java applications. The theory is that you can send your logging messages to any number of Appenders. Usually this is just to a file and to the console 99% of the time. With a little bit of magic though you can get certain log messages sent via email:

<appender name="WsosEmailLogger" class="">
     <param name="To" value=""/>
     <param name="From" value=""/>
     <param name="Subject" value="SSO alert"/>
     <param name="SMTPHost" value=""/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
   <appender name="AsyncWsosEmailLogger" class="org.apache.log4j.AsyncAppender">
     <appender-ref ref="WsosEmailLogger"/>

These are the appenders. Notice that there is an extra org.apache.log4j.AsyncAppender at the end. This is the actual appender you want to log to because otherwise you'll be waiting to send an email whenever you log something!

   <category name="WsosAuthLogger">
      <appender-ref ref="AsyncWsosEmailLogger"/>

By default the emai logger will only email ERROR level or above messages. Now you just need to log something to this category:

private static final Logger AUTH_LOGGER = Logger.getLogger("WsosAuthLogger");
AUTH_LOGGER.error("Auth failed!");

- 6 comments by 2 or more people Not publicly viewable

[Skip to the latest comment]
  1. Chris May

    Neat. If you set a sensible threshold (ERROR or FATAL is good) on the async appeneder, you can install it as one of the root appenders, and get email notifications any time the app logs an error, which is handy (though you have to be prepared to come in after a weekend and find 10,000 error messages in your inbox…)

    17 Oct 2005, 14:12

  2. Eray

    So; is there a way to stop these 10.000 mail.
    Can we create a rule like "Send Email to me for each 1000 Error Log"??? on the appendar?

    24 Nov 2005, 11:55

  3. Yes, there is the buffer size which will only send an email for however many log messages.

    <param name="BufferSize" value="100" />

    24 Nov 2005, 12:08

  4. Mike Colegrove

    The above is alright for log errors in a loop.

    What if you need something that sends all ERROR only log messages that occur within say 30 seconds when their are up to 10 and you really want these mailed to you.

    However if over 10 messages hit the buffer, within say 30 secs, them route them as one possibly to a different Appender.

    That way problems that invoke errors in a loop (e.g 10,000 errors) can be used to route to an SMS messager/Pager alert.

    05 Sep 2006, 17:02

  5. Steve

    Hadn’t thought about wrapping the SMTP Appender in an Async Appender. Good idea.

    A bit more info on the SMTP Appender here –

    24 Sep 2006, 08:08

  6. Dev M.

    If you really want a handle on your log4j behavior, take a look at using TriggeringEventEvaluator interface.

    07 Dec 2006, 22:51

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

August 2005

Mo Tu We Th Fr Sa Su
Jul |  Today  | Sep
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31            


Search this blog

Most recent comments

  • One thing that was glossed over is that if you use Spring, there is a filter you can put in your XML… by Mathew Mannion on this entry
  • You are my hero. by Mathew Mannion on this entry
  • And may all your chickens come home to roost – in a nice fluffy organic, non–supermarket farmed kind… by Julie Moreton on this entry
  • Good luck I hope that you enjoy the new job! by on this entry
  • Good luck Kieran. :) by on this entry


Not signed in
Sign in

Powered by BlogBuilder