You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by YOSHIOKA Naozo <Na...@fujixerox.co.jp> on 2002/01/16 05:56:59 UTC

SMTPAppender Enhancement/Source Code Integration Request

I think it would be better if SMTPAppender itself has the feature described below
though I could implement the subclass of SMTPAppender having the feature.

Could anyone please think about integrating the changes to the original source?

If you have any questions, please send me e-mail.
Thanks very much.


*Added Feature

SMTPAppender doesn't send any e-mail in the specified interval
after the latest sending if the buffer is not full
but puts logging events into the buffer only.


*Why I Need The Feature

You may receive 20 e-mails in 10 seconds and
each mail may contain only 1 logging event.

For example, fatal errors may keep being logged
unless the administrator does something,
You receive many e-mails containg only 1 same logging event.
Basically I think one e-mail per 20 or 30 minutes is enough.

You may think we can write the custom class of TriggeringEventEvaluator
to avoid it. But it's not enough.
The current SMTPAppender can send e-mails
only when append(LoggingEvent) is invoked.
So if TriggeringEventEvaluator decides not to send an e-mail now,
the logging event is not sent until the next logging event.
It may be long time like 20 or 30 minutes later
and the e-mail may contain the logging event
the administrator has to do something urgently.


*Source Code Modification

org/apache/log4j/net/SMTPAppender.java

See below for diff. See the attached file for modified source.

54a55
>   private int interval;
58a60,62
>   protected Scheduler scheduler;
>   protected long sentTime;
>   protected long timeout;
166a171,173
>     scheduler = new Scheduler();
>     scheduler.setDaemon(true);
>     scheduler.start();
264d270
<   synchronized
267c273,299
<     this.closed = true;
---
>     synchronized(this) {
>       if (closed) {
>       return;
>       }
>       closed = true;
>       if (cb.length() > 0) {
>       sendMail();
>         // Or
>         // sentTime = 0; sendBuffer();
>         // for backwad compatibility
>         // because SMTPAppender may have subclasses
>         // and sendBuffer() may be overridden?
>       }
>     }
>     try {
>       scheduler.interrupt();
>     } catch(SecurityException e) {
>       LogLog.error("Got a SecurityException while interrupting for the "+
>                  "scheduler to finish.", e);
>     }
>     try {
>       scheduler.join();
>     } catch(InterruptedException e) {
>       LogLog.error("Got an InterruptedException while waiting for the "+
>                  "scheduler to finish.", e);
>     }
>     scheduler = null;
306a339,356
>   protected
>   void sendBuffer() {
>     try {
>       if (cb.length() >= cb.getMaxSize()) {
>       if (timeout > 0) {      // scheduler is waiting timeout.
>         scheduler.interrupt();
>       }
>       sendMail();
>       } else {
>       if (timeout == 0) {     // scheduler is waiting to be scheduled.
>         scheduler.interrupt();
>       }
>       }
>     } catch (Exception e) {
>       LogLog.error("Error occured while sending e-mail notification.", e);
>     }
>   }
>
311c361
<   void sendBuffer() {
---
>   void sendMail() {
331a382
>             sbuf.append(Layout.LINE_SEP);
346a398,399
>
>       sentTime = System.currentTimeMillis();
513a567,602
>   }
>
>   public
>   void setInterval(int interval) {
>     this.interval = interval;
>   }
>
>   public
>   int getInterval() {
>     return interval;
>   }
>
>   protected class Scheduler extends Thread {
>
>     protected Scheduler() {
>     }
>
>     public void run() {
>       synchronized (SMTPAppender.this) {
>       while (!closed) {
>         timeout = sentTime + interval * 1000 -
>                                       System.currentTimeMillis();
>         if (cb.length() > 0 && timeout < 0) {
>           SMTPAppender.this.sendMail();
>         } else {
>           try {
>             if (cb.length() <= 0 || timeout < 0) {
>               timeout = 0;
>             }
>             SMTPAppender.this.wait(timeout);
>           } catch (InterruptedException e) {
>           }
>         }
>       }
>       }
>     }


*Test

I tested the modified code using a XML configuration file.
I put
 <param name="Interval" value="10"/>
with or without
 <param name="BufferSize" value="4"/>
then I ran the test program on jdk-1.3.1 with javamail-1.2 and jaf-1.0.1.
I think it worked fine.


Ceki, thank you for your reply of the e-mail, "I18n of SMTPAppender and FileAppender".
I'm happy of that.


Naozo