1 package org.codehaus.plexus.logging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.HashMap;
20 import java.util.Locale;
21 import java.util.Map;
22
23 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
24
25
26
27
28
29
30 public abstract class BaseLoggerManager extends AbstractLoggerManager implements Initializable {
31 private Map loggerCache = new HashMap();
32
33 private String threshold = "info";
34
35 private int currentThreshold;
36
37 public void initialize() {
38 currentThreshold = parseThreshold(threshold);
39
40 if (currentThreshold == -1) {
41 currentThreshold = Logger.LEVEL_DEBUG;
42 }
43 }
44
45 protected int parseThreshold(String text) {
46 text = text.trim().toLowerCase(Locale.ENGLISH);
47
48 if (text.equals("debug")) {
49 return Logger.LEVEL_DEBUG;
50 } else if (text.equals("info")) {
51 return Logger.LEVEL_INFO;
52 } else if (text.equals("warn")) {
53 return Logger.LEVEL_WARN;
54 } else if (text.equals("error")) {
55 return Logger.LEVEL_ERROR;
56 } else if (text.equals("fatal")) {
57 return Logger.LEVEL_FATAL;
58 }
59
60 return -1;
61 }
62
63
64
65
66
67
68
69 public void setThreshold(int currentThreshold) {
70 this.currentThreshold = currentThreshold;
71 }
72
73
74
75
76
77
78
79 public void setThresholds(int currentThreshold) {
80 this.currentThreshold = currentThreshold;
81
82 for (Object o : loggerCache.values()) {
83 Logger logger = (Logger) o;
84 logger.setThreshold(currentThreshold);
85 }
86 }
87
88
89
90
91
92
93 public int getThreshold() {
94 return currentThreshold;
95 }
96
97 public void setThreshold(String role, String roleHint, int threshold) {
98 AbstractLogger logger;
99
100 String key = toMapKey(role, roleHint);
101
102 logger = (AbstractLogger) loggerCache.get(key);
103
104 if (logger == null) {
105 return;
106 }
107
108 logger.setThreshold(threshold);
109 }
110
111 public int getThreshold(String role, String roleHint) {
112 AbstractLogger logger;
113
114 String key = toMapKey(role, roleHint);
115
116 logger = (AbstractLogger) loggerCache.get(key);
117
118 if (logger == null) {
119 return Logger.LEVEL_DEBUG;
120 }
121
122 return logger.getThreshold();
123 }
124
125 public Logger getLoggerForComponent(String role, String roleHint) {
126 Logger logger;
127
128 String key = toMapKey(role, roleHint);
129
130 logger = (Logger) loggerCache.get(key);
131
132 if (logger != null) {
133 return logger;
134 }
135
136 logger = createLogger(key);
137
138 loggerCache.put(key, logger);
139
140 return logger;
141 }
142
143 protected abstract Logger createLogger(String key);
144
145 public void returnComponentLogger(String role, String roleHint) {
146 Object obj;
147
148 String key = toMapKey(role, roleHint);
149
150 obj = loggerCache.remove(key);
151
152 if (obj == null) {
153
154 System.err.println("There was no such logger '" + key + "' " + hashCode() + ".");
155 }
156 }
157
158 public int getActiveLoggerCount() {
159 return loggerCache.size();
160 }
161
162 public String getThresholdAsString() {
163 return threshold;
164 }
165 }