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