1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core;
15
16 import java.util.List;
17
18 import ch.qos.logback.core.filter.Filter;
19 import ch.qos.logback.core.spi.ContextAwareBase;
20 import ch.qos.logback.core.spi.FilterAttachableImpl;
21 import ch.qos.logback.core.spi.FilterReply;
22 import ch.qos.logback.core.status.WarnStatus;
23
24
25
26
27
28
29
30
31
32 abstract public class AppenderBase<E> extends ContextAwareBase implements
33 Appender<E> {
34
35
36
37
38
39 protected Layout<E> layout;
40
41 protected boolean started = false;
42
43
44
45
46
47 private boolean guard = false;
48
49
50
51
52 protected String name;
53
54 private FilterAttachableImpl<E> fai = new FilterAttachableImpl<E>();
55
56 public String getName() {
57 return name;
58 }
59
60 private int statusRepeatCount = 0;
61 private int exceptionCount = 0;
62
63 static final int ALLOWED_REPEATS = 5;
64
65 public synchronized void doAppend(E eventObject) {
66
67
68
69
70 if (guard) {
71 return;
72 }
73
74 try {
75 guard = true;
76
77 if (!this.started) {
78 if (statusRepeatCount++ < ALLOWED_REPEATS) {
79 addStatus(new WarnStatus(
80 "Attempted to append to non started appender [" + name + "].",
81 this));
82 }
83 return;
84 }
85
86 if (getFilterChainDecision(eventObject) == FilterReply.DENY) {
87 return;
88 }
89
90
91 this.append(eventObject);
92
93 } catch (Exception e) {
94 if (exceptionCount++ < ALLOWED_REPEATS) {
95 addError("Appender [" + name + "] failed to append.", e);
96 }
97 } finally {
98 guard = false;
99 }
100 }
101
102 abstract protected void append(E eventObject);
103
104
105
106
107 public void setName(String name) {
108 this.name = name;
109 }
110
111 public void start() {
112 started = true;
113 }
114
115 public void stop() {
116 started = false;
117 }
118
119 public boolean isStarted() {
120 return started;
121 }
122
123 public String toString() {
124 return this.getClass().getName() + "[" + name + "]";
125 }
126
127 public void addFilter(Filter<E> newFilter) {
128 fai.addFilter(newFilter);
129 }
130
131 public Filter getFirstFilter() {
132 return fai.getFirstFilter();
133 }
134
135 public void clearAllFilters() {
136 fai.clearAllFilters();
137 }
138
139 public List<Filter<E>> getCopyOfAttachedFiltersList() {
140 return fai.getCopyOfAttachedFiltersList();
141 }
142
143 public FilterReply getFilterChainDecision(E event) {
144 return fai.getFilterChainDecision(event);
145 }
146
147
148
149
150
151 public Layout<E> getLayout() {
152 return layout;
153 }
154
155
156
157
158
159 public void setLayout(Layout<E> layout) {
160 this.layout = layout;
161 }
162 }