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.classic.net;
15  
16  import ch.qos.logback.classic.ClassicConstants;
17  import ch.qos.logback.classic.PatternLayout;
18  import ch.qos.logback.classic.boolex.OnErrorEvaluator;
19  import ch.qos.logback.classic.spi.ILoggingEvent;
20  import ch.qos.logback.core.Layout;
21  import ch.qos.logback.core.boolex.EventEvaluator;
22  import ch.qos.logback.core.helpers.CyclicBuffer;
23  import ch.qos.logback.core.net.SMTPAppenderBase;
24  import org.slf4j.Marker;
25  
26  /**
27   * Send an e-mail when a specific logging event occurs, typically on errors or
28   * fatal errors.
29   * 
30   * For more information about this appender, please refer to the online manual at
31   * http://logback.qos.ch/manual/appenders.html#SMTPAppender
32   * 
33   * @author Ceki Gülcü
34   * @author Sébastien Pennec
35   * 
36   */
37  public class SMTPAppender extends SMTPAppenderBase<ILoggingEvent> {
38  
39      // value "%logger{20} - %m" is referenced in the docs!
40      static final String DEFAULT_SUBJECT_PATTERN = "%logger{20} - %m";
41  
42      private int bufferSize = 512;
43      private boolean includeCallerData = false;
44  
45      /**
46       * The default constructor will instantiate the appender with a
47       * {@link EventEvaluator} that will trigger on events with level
48       * ERROR or higher.
49       */
50      public SMTPAppender() {
51  
52      }
53  
54      public void start() {
55          if (eventEvaluator == null) {
56              OnErrorEvaluator onError = new OnErrorEvaluator();
57              onError.setContext(getContext());
58              onError.setName("onError");
59              onError.start();
60              this.eventEvaluator = onError;
61          }
62          super.start();
63      }
64  
65      /**
66       * Use the parameter as the {@link
67       * EventEvaluator} for this SMTPAppender.
68       */
69      public SMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) {
70          this.eventEvaluator = eventEvaluator;
71      }
72  
73      /**
74       * Perform SMTPAppender specific appending actions, mainly adding the event to
75       * a cyclic buffer.
76       */
77      protected void subAppend(CyclicBuffer<ILoggingEvent> cb, ILoggingEvent event) {
78          if (includeCallerData) {
79              event.getCallerData();
80          }
81          event.prepareForDeferredProcessing();
82          cb.add(event);
83      }
84  
85      @Override
86      protected void fillBuffer(CyclicBuffer<ILoggingEvent> cb, StringBuffer sbuf) {
87          int len = cb.length();
88          for (int i = 0; i < len; i++) {
89              ILoggingEvent event = cb.get();
90              sbuf.append(layout.doLayout(event));
91          }
92      }
93  
94      protected boolean eventMarksEndOfLife(ILoggingEvent eventObject) {
95          Marker marker = eventObject.getMarker();
96          if (marker == null)
97              return false;
98  
99          return marker.contains(ClassicConstants.FINALIZE_SESSION_MARKER);
100     }
101 
102     @Override
103     protected Layout<ILoggingEvent> makeSubjectLayout(String subjectStr) {
104         if (subjectStr == null) {
105             subjectStr = DEFAULT_SUBJECT_PATTERN;
106         }
107         PatternLayout pl = new PatternLayout();
108         pl.setContext(getContext());
109         pl.setPattern(subjectStr);
110         // we don't want a ThrowableInformationConverter appended
111         // to the end of the converter chain
112         // This fixes issue LBCLASSIC-67
113         pl.setPostCompileProcessor(null);
114         pl.start();
115         return pl;
116     }
117 
118     protected PatternLayout makeNewToPatternLayout(String toPattern) {
119         PatternLayout pl = new PatternLayout();
120         pl.setPattern(toPattern + "%nopex");
121         return pl;
122     }
123 
124     public boolean isIncludeCallerData() {
125         return includeCallerData;
126     }
127 
128     public void setIncludeCallerData(boolean includeCallerData) {
129         this.includeCallerData = includeCallerData;
130     }
131 }