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 boolean includeCallerData = false;
43  
44      /**
45       * The default constructor will instantiate the appender with a
46       * {@link EventEvaluator} that will trigger on events with level
47       * ERROR or higher.
48       */
49      public SMTPAppender() {
50  
51      }
52  
53      public void start() {
54          if (eventEvaluator == null) {
55              OnErrorEvaluator onError = new OnErrorEvaluator();
56              onError.setContext(getContext());
57              onError.setName("onError");
58              onError.start();
59              this.eventEvaluator = onError;
60          }
61          super.start();
62      }
63  
64      /**
65       * Use the parameter as the {@link
66       * EventEvaluator} for this SMTPAppender.
67       */
68      public SMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) {
69          this.eventEvaluator = eventEvaluator;
70      }
71  
72      /**
73       * Perform SMTPAppender specific appending actions, mainly adding the event to
74       * a cyclic buffer.
75       */
76      protected void subAppend(CyclicBuffer<ILoggingEvent> cb, ILoggingEvent event) {
77          if (includeCallerData) {
78              event.getCallerData();
79          }
80          event.prepareForDeferredProcessing();
81          cb.add(event);
82      }
83  
84      @Override
85      protected void fillBuffer(CyclicBuffer<ILoggingEvent> cb, StringBuffer sbuf) {
86          int len = cb.length();
87          for (int i = 0; i < len; i++) {
88              ILoggingEvent event = cb.get();
89              sbuf.append(layout.doLayout(event));
90          }
91      }
92  
93      protected boolean eventMarksEndOfLife(ILoggingEvent eventObject) {
94          Marker marker = eventObject.getMarker();
95          if (marker == null)
96              return false;
97  
98          return marker.contains(ClassicConstants.FINALIZE_SESSION_MARKER);
99      }
100 
101     @Override
102     protected Layout<ILoggingEvent> makeSubjectLayout(String subjectStr) {
103         if (subjectStr == null) {
104             subjectStr = DEFAULT_SUBJECT_PATTERN;
105         }
106         PatternLayout pl = new PatternLayout();
107         pl.setContext(getContext());
108         pl.setPattern(subjectStr);
109         // we don't want a ThrowableInformationConverter appended
110         // to the end of the converter chain
111         // This fixes issue LBCLASSIC-67
112         pl.setPostCompileProcessor(null);
113         pl.start();
114         return pl;
115     }
116 
117     protected PatternLayout makeNewToPatternLayout(String toPattern) {
118         PatternLayout pl = new PatternLayout();
119         pl.setPattern(toPattern + "%nopex");
120         return pl;
121     }
122 
123     public boolean isIncludeCallerData() {
124         return includeCallerData;
125     }
126 
127     public void setIncludeCallerData(boolean includeCallerData) {
128         this.includeCallerData = includeCallerData;
129     }
130 }