View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 certain
25   * pages are accessed.
26   * 
27   * For more information about this appender, please refer to the online manual
28   * at http://logback.qos.ch/manual/appenders.html#AccessSMTPAppender
29   * <p>
30   * 
31   * @author Ceki G&uuml;lc&uuml;
32   * @author S&eacute;bastien Pennec
33   * 
34   */
35  public class SMTPAppender extends SMTPAppenderBase<IAccessEvent> {
36  
37      static final String DEFAULT_SUBJECT_PATTERN = "%m";
38  
39      /**
40       * The default constructor will instantiate the appender with a
41       * {@link EventEvaluator} that will trigger on events with level ERROR or
42       * higher.
43       */
44      public SMTPAppender() {
45      }
46  
47      /**
48       * Use <code>evaluator</code> passed as parameter as the {@link EventEvaluator}
49       * for this SMTPAppender.
50       */
51      public SMTPAppender(EventEvaluator<IAccessEvent> evaluator) {
52          this.eventEvaluator = evaluator;
53      }
54  
55      /**
56       * Perform SMTPAppender specific appending actions, mainly adding the event to
57       * the appropriate cyclic buffer.
58       */
59      @Override
60      protected void subAppend(CyclicBuffer<IAccessEvent> cb, IAccessEvent event) {
61          cb.add(event);
62      }
63  
64      @Override
65      protected void fillBuffer(CyclicBuffer<IAccessEvent> cb, StringBuffer sbuf) {
66          int len = cb.length();
67          for (int i = 0; i < len; i++) {
68              // sbuf.append(MimeUtility.encodeText(layout.format(cb.getOrCreate())));
69              IAccessEvent event = cb.get();
70              sbuf.append(layout.doLayout(event));
71          }
72      }
73  
74      @Override
75      protected Layout<IAccessEvent> makeSubjectLayout(String subjectStr) {
76          if (subjectStr == null) {
77              subjectStr = DEFAULT_SUBJECT_PATTERN;
78          }
79          PatternLayout pl = new PatternLayout();
80          pl.setPattern(subjectStr);
81          pl.start();
82          return pl;
83      }
84  
85      @Override
86      protected PatternLayout makeNewToPatternLayout(String toPattern) {
87          PatternLayout pl = new PatternLayout();
88          pl.setPattern(toPattern);
89          return pl;
90      }
91  
92      @Override
93      protected boolean eventMarksEndOfLife(IAccessEvent eventObject) {
94          return false;
95      }
96  
97  }