1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.control;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import ch.qos.logback.classic.Level;
20 import ch.qos.logback.core.CoreConstants;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class ControlLoggerContext {
42
43 private ControlLogger root;
44
45
46 Map<String, ControlLogger> loggerMap = new HashMap<String, ControlLogger>();
47
48 public ControlLoggerContext() {
49 this.root = new ControlLogger("root", null);
50 this.root.setLevel(Level.DEBUG);
51 }
52
53
54
55
56
57
58 public ControlLogger getRootLogger() {
59 return root;
60 }
61
62 public ControlLogger exists(String name) {
63 if (name == null) {
64 throw new IllegalArgumentException("name parameter cannot be null");
65 }
66
67 synchronized (loggerMap) {
68 return (ControlLogger) loggerMap.get(name);
69 }
70 }
71
72 public final ControlLogger getLogger(String name) {
73 if (name == null) {
74 throw new IllegalArgumentException("name parameter cannot be null");
75 }
76
77 synchronized (loggerMap) {
78 ControlLogger cl = (ControlLogger) loggerMap.get(name);
79 if (cl != null) {
80 return cl;
81 }
82 ControlLogger parent = this.root;
83
84 int i = 0;
85 while (true) {
86 i = name.indexOf(CoreConstants.DOT, i);
87 if (i == -1) {
88
89
90 cl = new ControlLogger(name, parent);
91 loggerMap.put(name, cl);
92 return cl;
93 } else {
94 String parentName = name.substring(0, i);
95 ControlLogger p = (ControlLogger) loggerMap.get(parentName);
96 if (p == null) {
97
98
99 p = new ControlLogger(parentName, parent);
100 loggerMap.put(parentName, p);
101 }
102 parent = p;
103 }
104
105 i++;
106 }
107 }
108 }
109
110 public Map<String, ControlLogger> getLoggerMap() {
111 return loggerMap;
112 }
113 }