1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import org.slf4j.helpers.MarkerIgnoringBase;
22
23 import ch.qos.logback.classic.spi.ILoggingEvent;
24 import ch.qos.logback.core.Appender;
25 import ch.qos.logback.core.CoreConstants;
26
27 public class HLogger extends MarkerIgnoringBase {
28
29 private static final long serialVersionUID = 1L;
30
31 static int instanceCount = 0;
32
33
34
35
36 private String name;
37
38
39 private Level level;
40
41
42
43 private Level effectiveLevel;
44
45
46
47
48
49 HLogger parent;
50
51
52
53
54 Map<String, HLogger> childrenMap;
55
56
57
58
59 private ArrayList<Appender<ILoggingEvent>> appenderList;
60
61
62
63
64
65
66
67
68
69 protected boolean additive = true;
70
71 HLogger(String name, HLogger parent) {
72 this.name = name;
73 this.parent = parent;
74 instanceCount++;
75 }
76
77 Level getEffectiveLevel() {
78 return effectiveLevel;
79 }
80
81 Level getLevel() {
82 return level;
83 }
84
85 public String getName() {
86 return name;
87 }
88
89 private boolean isRootLogger() {
90
91 return parent == null;
92 }
93
94
95
96
97
98
99
100
101
102
103
104 HLogger getChildBySuffix(final String suffix) {
105 if (childrenMap == null) {
106 return null;
107 } else {
108 return (HLogger) childrenMap.get(suffix);
109 }
110 }
111
112 public synchronized void setLevel(Level newLevel) {
113 if (level == newLevel) {
114
115 return;
116 }
117
118 level = newLevel;
119 effectiveLevel = newLevel;
120 if (childrenMap != null) {
121 for (Iterator<HLogger> i = childrenMap.values().iterator(); i.hasNext();) {
122 HLogger child = (HLogger) i.next();
123
124
125 child.handleParentLevelChange(effectiveLevel);
126 }
127 }
128 }
129
130
131
132
133
134
135
136 private synchronized void handleParentLevelChange(Level newParentLevel) {
137
138
139 if (level == null) {
140 effectiveLevel = newParentLevel;
141
142
143 if (childrenMap != null) {
144 for (Iterator<HLogger> i = childrenMap.values().iterator(); i.hasNext();) {
145 HLogger child = (HLogger) i.next();
146
147 child.handleParentLevelChange(effectiveLevel);
148 }
149 }
150 }
151 }
152
153
154
155
156
157
158 public synchronized void removeAllAppenders() {
159 if (appenderList != null) {
160 int len = appenderList.size();
161 for (int i = 0; i < len; i++) {
162 Appender<ILoggingEvent> a = appenderList.get(i);
163 a.stop();
164 }
165 appenderList.clear();
166 appenderList = null;
167 }
168 }
169
170
171
172
173
174
175 public void callAppenders(ILoggingEvent event) {
176
177
178 for (HLogger l = this; l != null; l = l.parent) {
179
180 synchronized (l) {
181 if (l.appenderList != null) {
182 l.appendLoopOnAppenders(event);
183 }
184 if (!l.additive) {
185 break;
186 }
187 }
188 }
189
190
191
192
193
194
195
196
197 }
198
199 private int appendLoopOnAppenders(ILoggingEvent event) {
200 int size = 0;
201 Appender<ILoggingEvent> appender;
202
203 if (appenderList != null) {
204 size = appenderList.size();
205 for (int i = 0; i < size; i++) {
206 appender = appenderList.get(i);
207 appender.doAppend(event);
208 }
209 }
210 return size;
211 }
212
213
214
215
216 public synchronized void removeAppender(Appender<ILoggingEvent> appender) {
217 if ((appender == null) || (appenderList == null)) {
218 }
219 appenderList.remove(appender);
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 HLogger createChildByLastNamePart(final String lastPart) {
237 int i_index = lastPart.indexOf(CoreConstants.DOT);
238 if (i_index != -1) {
239 throw new IllegalArgumentException(
240 "Child name [" + lastPart + " passed as parameter, may not include [" + CoreConstants.DOT + "]");
241 }
242
243 if (childrenMap == null) {
244 childrenMap = new HashMap<String, HLogger>(2);
245 }
246 HLogger childHLogger;
247 if (this.isRootLogger()) {
248 childHLogger = new HLogger(lastPart, this);
249 } else {
250 childHLogger = new HLogger(name + CoreConstants.DOT + lastPart, this);
251 }
252 childrenMap.put(lastPart, childHLogger);
253 childHLogger.effectiveLevel = this.effectiveLevel;
254 return childHLogger;
255 }
256
257 public final void trace(String msg) {
258 if (effectiveLevel.levelInt <= Level.TRACE_INT) {
259 throw new UnsupportedOperationException("not yet implemented");
260 }
261 }
262
263 public void trace(String msg, Throwable t) {
264
265
266 }
267
268 public void trace(Object parameterizedMsg, Object param1) {
269
270
271 }
272
273 public void trace(String parameterizedMsg, Object param1, Object param2) {
274
275
276 }
277
278 public final void debug(String msg) {
279 if (effectiveLevel.levelInt <= Level.DEBUG_INT) {
280 throw new UnsupportedOperationException("not yet implemented");
281 }
282 }
283
284 public void debug(String msg, Throwable t) {
285
286
287 }
288
289 public void debug(Object parameterizedMsg, Object param1) {
290
291
292 }
293
294 public void debug(String parameterizedMsg, Object param1, Object param2) {
295
296
297 }
298
299 public void error(String msg) {
300
301
302 }
303
304 public void error(String msg, Throwable t) {
305
306
307 }
308
309 public void error(String parameterizedMsg, Object param1) {
310
311
312 }
313
314 public void error(String parameterizedMsg, Object param1, Object param2) {
315
316
317 }
318
319 public void info(String msg) {
320
321
322 }
323
324 public void info(String msg, Throwable t) {
325
326
327 }
328
329 public void info(String parameterizedMsg, Object param1) {
330
331
332 }
333
334 public void info(String parameterizedMsg, Object param1, Object param2) {
335
336
337 }
338
339 public boolean isTraceEnabled() {
340 return false;
341 }
342
343 public boolean isDebugEnabled() {
344 return false;
345 }
346
347 public boolean isErrorEnabled() {
348 return false;
349
350 }
351
352 public boolean isInfoEnabled() {
353 return false;
354
355 }
356
357 public boolean isWarnEnabled() {
358 return false;
359
360 }
361
362 public void warn(String msg) {
363
364
365 }
366
367 public void warn(String msg, Throwable t) {
368
369
370 }
371
372 public void warn(String parameterizedMsg, Object param1) {
373
374
375 }
376
377 public void warn(String parameterizedMsg, Object param1, Object param2) {
378
379
380 }
381
382 public void trace(String format, Object arg) {
383 }
384
385 public void trace(String format, Object... argArray) {
386 }
387
388 public void debug(String format, Object arg) {
389 }
390
391 public void debug(String format, Object... argArray) {
392 }
393
394 public void info(String format, Object... argArray) {
395 }
396
397 public void warn(String format, Object... argArray) {
398 }
399
400 public void error(String format, Object... argArray) {
401 }
402 }