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