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
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      @Override
59      protected void subAppend(CyclicBuffer<IAccessEvent> cb, IAccessEvent event) {
60          cb.add(event);
61      }
62  
63      @Override
64      protected void fillBuffer(CyclicBuffer<IAccessEvent> cb, StringBuffer sbuf) {
65          int len = cb.length();
66          for (int i = 0; i < len; i++) {
67              // sbuf.append(MimeUtility.encodeText(layout.format(cb.getOrCreate())));
68              IAccessEvent event = cb.get();
69              sbuf.append(layout.doLayout(event));
70          }
71      }
72  
73      @Override
74      protected Layout<IAccessEvent> makeSubjectLayout(String subjectStr) {
75          if (subjectStr == null) {
76              subjectStr = DEFAULT_SUBJECT_PATTERN;
77          }
78          PatternLayout pl = new PatternLayout();
79          pl.setPattern(subjectStr);
80          pl.start();
81          return pl;
82      }
83  
84      @Override
85      protected PatternLayout makeNewToPatternLayout(String toPattern) {
86          PatternLayout pl = new PatternLayout();
87          pl.setPattern(toPattern);
88          return pl;
89      }
90  
91      @Override
92      protected boolean eventMarksEndOfLife(IAccessEvent eventObject) {
93          return false;
94      }
95  
96  }