All entries for Thursday 18 August 2005

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="org.apache.log4j.net.SMTPAppender">
     <param name="To" value="someadminemail@test.com"/>
     <param name="From" value="someadminemail@test.com"/>
     <param name="Subject" value="SSO alert"/>
     <param name="SMTPHost" value="mail.warwick.ac.uk"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
     </layout>
   </appender>
   
   <appender name="AsyncWsosEmailLogger" class="org.apache.log4j.AsyncAppender">
     <appender-ref ref="WsosEmailLogger"/>
   </appender>

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"/>
   </category>

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!");

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            

Tags

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

Galleries

Not signed in
Sign in

Powered by BlogBuilder
© MMXIX