1 package org.codehaus.plexus.logging.console;
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.logging.AbstractLoggerManager;
24 import org.codehaus.plexus.logging.Logger;
25 import org.codehaus.plexus.logging.LoggerManager;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class ConsoleLoggerManager extends AbstractLoggerManager implements LoggerManager, Initializable {
45
46
47
48
49
50
51 private String threshold = "info";
52
53 private int currentThreshold;
54
55 private Map loggers;
56
57
58 private int loggerCount;
59
60 private boolean bootTimeLogger = false;
61
62 public ConsoleLoggerManager() {}
63
64
65
66
67
68 public ConsoleLoggerManager(String threshold) {
69 this.threshold = threshold;
70
71 bootTimeLogger = true;
72
73 initialize();
74 }
75
76 public void initialize() {
77 debug("Initializing ConsoleLoggerManager: " + this.hashCode() + ".");
78
79
80 currentThreshold = parseThreshold(threshold);
81
82 if (currentThreshold == -1) {
83 debug("Could not parse the threshold level: '" + threshold + "', setting to debug.");
84 currentThreshold = Logger.LEVEL_DEBUG;
85 }
86
87 loggers = new HashMap();
88 }
89
90 public void setThreshold(int currentThreshold) {
91 this.currentThreshold = currentThreshold;
92 }
93
94 public void setThresholds(int currentThreshold) {
95 this.currentThreshold = currentThreshold;
96
97 for (Object o : loggers.values()) {
98 Logger logger = (Logger) o;
99 logger.setThreshold(currentThreshold);
100 }
101 }
102
103
104
105
106 public int getThreshold() {
107 return currentThreshold;
108 }
109
110
111
112 public void setThreshold(String role, String roleHint, int threshold) {
113 ConsoleLogger logger;
114 String name;
115
116 name = toMapKey(role, roleHint);
117 logger = (ConsoleLogger) loggers.get(name);
118
119 if (logger == null) {
120 debug("Trying to set the threshold of a unknown logger '" + name + "'.");
121 return;
122 }
123
124 logger.setThreshold(threshold);
125 }
126
127 public int getThreshold(String role, String roleHint) {
128 ConsoleLogger logger;
129 String name;
130
131 name = toMapKey(role, roleHint);
132 logger = (ConsoleLogger) loggers.get(name);
133
134 if (logger == null) {
135 debug("Trying to get the threshold of a unknown logger '" + name + "'.");
136 return Logger.LEVEL_DEBUG;
137 }
138
139 return logger.getThreshold();
140 }
141
142 public Logger createLogger(int threshold, String name) {
143 return new ConsoleLogger(threshold, name);
144 }
145
146 public Logger getLoggerForComponent(String role, String roleHint) {
147 Logger logger;
148 String name;
149
150 name = toMapKey(role, roleHint);
151 logger = (Logger) loggers.get(name);
152
153 if (logger != null) return logger;
154
155 debug("Creating logger '" + name + "' " + this.hashCode() + ".");
156 logger = createLogger(getThreshold(), name);
157 loggers.put(name, logger);
158
159 return logger;
160 }
161
162 public void returnComponentLogger(String role, String roleHint) {
163 Object obj;
164 String name;
165
166 name = toMapKey(role, roleHint);
167 obj = loggers.remove(name);
168
169 if (obj == null) {
170 debug("There was no such logger '" + name + "' " + this.hashCode() + ".");
171 } else {
172 debug("Removed logger '" + name + "' " + this.hashCode() + ".");
173 }
174 }
175
176 public int getActiveLoggerCount() {
177 return loggers.size();
178 }
179
180 private int parseThreshold(String text) {
181 text = text.trim().toLowerCase(Locale.ENGLISH);
182
183 if (text.equals("debug")) {
184 return ConsoleLogger.LEVEL_DEBUG;
185 } else if (text.equals("info")) {
186 return ConsoleLogger.LEVEL_INFO;
187 } else if (text.equals("warn")) {
188 return ConsoleLogger.LEVEL_WARN;
189 } else if (text.equals("error")) {
190 return ConsoleLogger.LEVEL_ERROR;
191 } else if (text.equals("fatal")) {
192 return ConsoleLogger.LEVEL_FATAL;
193 }
194
195 return -1;
196 }
197
198 private String decodeLogLevel(int logLevel) {
199 switch (logLevel) {
200 case ConsoleLogger.LEVEL_DEBUG:
201 return "debug";
202 case ConsoleLogger.LEVEL_INFO:
203 return "info";
204 case ConsoleLogger.LEVEL_WARN:
205 return "warn";
206 case ConsoleLogger.LEVEL_ERROR:
207 return "error";
208 case ConsoleLogger.LEVEL_FATAL:
209 return "fatal";
210 case ConsoleLogger.LEVEL_DISABLED:
211 return "disabled";
212 default:
213 return "unknown";
214 }
215 }
216
217
218
219
220
221
222 private void debug(String msg) {
223
224
225 }
226 }