All 1 entries tagged Log4J

View all 2 entries tagged Log4J on Warwick Blogs | View entries tagged Log4J at Technorati | There are no images tagged Log4J on this blog

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

October 2022

Mo Tu We Th Fr Sa Su
Sep |  Today  |
               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


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