1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  public class SMTPAppender extends SMTPAppenderBase<ILoggingEvent> {
42  
43      
44      static final String DEFAULT_SUBJECT_PATTERN = "%logger{20} - %m";
45  
46      private boolean includeCallerData = false;
47  
48      
49  
50  
51  
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  
70  
71      public SMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) {
72          this.eventEvaluator = eventEvaluator;
73      }
74  
75      
76  
77  
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         
118         
119         
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 }