View Javadoc

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