1
2
3
4
5
6
7
8
9
10
11 package ch.qos.logback.classic.net;
12
13 import ch.qos.logback.classic.PatternLayout;
14 import ch.qos.logback.classic.boolex.OnErrorEvaluator;
15 import ch.qos.logback.classic.spi.LoggingEvent;
16 import ch.qos.logback.core.Layout;
17 import ch.qos.logback.core.boolex.EventEvaluator;
18 import ch.qos.logback.core.helpers.CyclicBuffer;
19 import ch.qos.logback.core.net.SMTPAppenderBase;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class SMTPAppender extends SMTPAppenderBase<LoggingEvent> {
33
34
35 static final String DEFAULT_SUBJECT_PATTERN = "%logger{20} - %m";
36
37 private int bufferSize = 512;
38 protected CyclicBuffer<LoggingEvent> cb = new CyclicBuffer<LoggingEvent>(bufferSize);
39
40
41
42
43
44
45 public SMTPAppender() {
46
47 }
48
49 public void start() {
50 if (eventEvaluator == null) {
51 OnErrorEvaluator onError = new OnErrorEvaluator();
52 onError.setContext(getContext());
53 onError.setName("onError");
54 onError.start();
55 this.eventEvaluator = onError;
56 }
57 super.start();
58 }
59
60
61
62
63
64 public SMTPAppender(EventEvaluator<LoggingEvent> eventEvaluator) {
65 this.eventEvaluator = eventEvaluator;
66 }
67
68
69
70
71
72 protected void subAppend(LoggingEvent event) {
73 event.prepareForDeferredProcessing();
74 cb.add(event);
75
76 }
77
78 @Override
79 protected void fillBuffer(StringBuffer sbuf) {
80 int len = cb.length();
81 for (int i = 0; i < len; i++) {
82
83 LoggingEvent event = cb.get();
84 sbuf.append(layout.doLayout(event));
85 }
86 }
87
88
89
90
91
92
93
94
95 public void setBufferSize(int bufferSize) {
96 this.bufferSize = bufferSize;
97 cb.resize(bufferSize);
98 }
99
100
101
102
103 public int getBufferSize() {
104 return bufferSize;
105 }
106
107 @Override
108 protected Layout<LoggingEvent> makeSubjectLayout(String subjectStr) {
109 if(subjectStr == null) {
110 subjectStr = DEFAULT_SUBJECT_PATTERN;
111 }
112 PatternLayout pl = new PatternLayout();
113 pl.setContext(getContext());
114 pl.setPattern(subjectStr);
115
116
117
118 pl.setPostCompileProcessor(null);
119 pl.start();
120 return pl;
121 }
122 }