View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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.IAccessEvent;
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<IAccessEvent> {
35  
36    static final String DEFAULT_SUBJECT_PATTERN = "%m";
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<IAccessEvent> evaluator) {
51      this.eventEvaluator = evaluator;
52    }
53  
54    /**
55     * Perform SMTPAppender specific appending actions, mainly adding the event to
56     * the appropriate cyclic buffer.
57     */
58    protected void subAppend(CyclicBuffer<IAccessEvent> cb, IAccessEvent event) {
59      cb.add(event);
60    }
61  
62    @Override
63    protected void fillBuffer(CyclicBuffer<IAccessEvent> cb, StringBuffer sbuf) {
64      int len = cb.length();
65      for (int i = 0; i < len; i++) {
66        // sbuf.append(MimeUtility.encodeText(layout.format(cb.getOrCreate())));
67        IAccessEvent event = cb.get();
68        sbuf.append(layout.doLayout(event));
69      }
70    }
71  
72    @Override
73    protected Layout<IAccessEvent> makeSubjectLayout(String subjectStr) {
74      if(subjectStr == null) {
75        subjectStr = DEFAULT_SUBJECT_PATTERN;
76      }
77      PatternLayout pl = new PatternLayout();
78      pl.setPattern(subjectStr);
79      pl.start();
80      return pl;
81    }
82  
83    protected PatternLayout makeNewToPatternLayout(String toPattern) {
84      PatternLayout pl = new PatternLayout();
85      pl.setPattern(toPattern);
86      return pl;
87    }
88  
89    protected boolean isEventMarkedForBufferRemoval(IAccessEvent eventObject) {
90      return false;
91    }
92  
93  }