View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.access.net;
12  
13  import ch.qos.logback.access.PatternLayout;
14  import ch.qos.logback.access.spi.AccessEvent;
15  import ch.qos.logback.core.Layout;
16  import ch.qos.logback.core.boolex.EventEvaluator;
17  import ch.qos.logback.core.helpers.CyclicBuffer;
18  import ch.qos.logback.core.net.SMTPAppenderBase;
19  
20  /**
21   * Send an e-mail when a specific access event occurs, typically when
22   * certain pages are accessed.
23   * 
24   * For more information about this appender, please refer to the online manual at
25   * http://logback.qos.ch/manual/appenders.html#AccessSMTPAppender
26   * <p>
27   * @author Ceki G&uuml;lc&uuml;
28   * @author S&eacute;bastien Pennec
29   * 
30   */
31  public class SMTPAppender extends SMTPAppenderBase<AccessEvent> {
32  
33    static final String DEFAULT_SUBJECT_PATTERN = "%m";
34    
35    private int bufferSize = 512;
36    protected CyclicBuffer<AccessEvent> cb = new CyclicBuffer<AccessEvent>(bufferSize);
37  
38    /**
39     * The default constructor will instantiate the appender with a
40     * {@link EventEvaluator} that will trigger on events with level
41     * ERROR or higher.
42     */
43    public SMTPAppender() {
44    }
45  
46    /**
47     * Use <code>evaluator</code> passed as parameter as the {@link
48     * EventEvaluator} for this SMTPAppender.
49     */
50    public SMTPAppender(EventEvaluator<AccessEvent> evaluator) {
51      this.eventEvaluator = evaluator;
52    }
53  
54    /**
55     * Perform SMTPAppender specific appending actions, mainly adding the event to
56     * a cyclic buffer.
57     */
58    protected void subAppend(AccessEvent event) {
59      cb.add(event);
60      // addInfo("Added event to the cyclic buffer: " + event.getMessage());
61    }
62  
63    @Override
64    protected void fillBuffer(StringBuffer sbuf) {
65      int len = cb.length();
66      for (int i = 0; i < len; i++) {
67        // sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));
68        AccessEvent event = (AccessEvent) cb.get();
69        sbuf.append(layout.doLayout(event));
70      }
71    }
72  
73    /**
74     * The <b>BufferSize</b> option takes a positive integer representing the
75     * maximum number of logging events to collect in a cyclic buffer. When the
76     * <code>BufferSize</code> is reached, oldest events are deleted as new
77     * events are added to the buffer. By default the size of the cyclic buffer is
78     * 512 events.
79     */
80    public void setBufferSize(int bufferSize) {
81      this.bufferSize = bufferSize;
82      cb.resize(bufferSize);
83    }
84  
85    /**
86     * Returns value of the <b>BufferSize</b> option.
87     */
88    public int getBufferSize() {
89      return bufferSize;
90    }
91  
92    @Override
93    protected Layout<AccessEvent> makeSubjectLayout(String subjectStr) {
94      if(subjectStr == null) {
95        subjectStr = DEFAULT_SUBJECT_PATTERN;
96      }
97      PatternLayout pl = new PatternLayout();
98      pl.setPattern(subjectStr);
99      pl.start();
100     return pl;
101   }
102 }