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 import ch.qos.logback.core.util.ReentryGuard;
24 import ch.qos.logback.core.util.ReentryGuardFactory;
25 import ch.qos.logback.core.util.SimpleTimeBasedGuard;
26
27
28
29
30
31
32
33
34 abstract public class UnsynchronizedAppenderBase<E> extends ContextAwareBase implements Appender<E> {
35
36 protected volatile boolean started = false;
37
38
39
40
41
42
43 private ReentryGuard reentryGuard;
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 SimpleTimeBasedGuard notStartedGuard = new SimpleTimeBasedGuard();
57 private SimpleTimeBasedGuard exceptionGuard = new SimpleTimeBasedGuard();
58
59
60 public void doAppend(E eventObject) {
61 if (!this.started) {
62
63 if (notStartedGuard.allow()) {
64 addStatus(new WarnStatus("Attempted to append to non started appender [" + name + "].", this));
65 }
66 return;
67 }
68
69
70 if (reentryGuard.isLocked()) {
71 return;
72 }
73
74 try {
75 reentryGuard.lock();
76
77 if (getFilterChainDecision(eventObject) == FilterReply.DENY) {
78 return;
79 }
80
81
82 this.append(eventObject);
83
84 } catch (Exception e) {
85 if (exceptionGuard.allow()) {
86 addError("Appender [" + name + "] failed to append.", e);
87 }
88 } finally {
89 reentryGuard.unlock();
90 }
91 }
92
93 abstract protected void append(E eventObject);
94
95
96
97
98 public void setName(String name) {
99 this.name = name;
100 }
101
102 public void start() {
103 this.reentryGuard = buildReentryGuard();
104 started = true;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 protected ReentryGuard buildReentryGuard() {
131 return ReentryGuardFactory.makeGuard(ReentryGuardFactory.GuardType.NOP);
132 }
133
134 public void stop() {
135 started = false;
136 }
137
138 public boolean isStarted() {
139 return started;
140 }
141
142 public String toString() {
143 return this.getClass().getName() + "[" + name + "]";
144 }
145
146 public void addFilter(Filter<E> newFilter) {
147 fai.addFilter(newFilter);
148 }
149
150 public void clearAllFilters() {
151 fai.clearAllFilters();
152 }
153
154 public List<Filter<E>> getCopyOfAttachedFiltersList() {
155 return fai.getCopyOfAttachedFiltersList();
156 }
157
158 public FilterReply getFilterChainDecision(E event) {
159 return fai.getFilterChainDecision(event);
160 }
161 }