1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.slf4j;
20
21 import java.io.PrintStream;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25
26 import org.slf4j.Logger;
27 import org.slf4j.Marker;
28 import org.slf4j.event.Level;
29 import org.slf4j.event.LoggingEvent;
30 import org.slf4j.helpers.LegacyAbstractLogger;
31 import org.slf4j.helpers.MessageFormatter;
32 import org.slf4j.helpers.NormalizedParameters;
33 import org.slf4j.spi.LocationAwareLogger;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public class MavenBaseLogger extends LegacyAbstractLogger {
145
146 private static final long serialVersionUID = -632788891211436180L;
147
148 private static final long START_TIME = System.currentTimeMillis();
149
150 protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
151 protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
152 protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
153 protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
154 protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
155
156 static final char SP = ' ';
157 static final String TID_PREFIX = "tid=";
158
159
160
161
162 protected static final int LOG_LEVEL_OFF = LOG_LEVEL_ERROR + 10;
163
164 static final SimpleLoggerConfiguration CONFIG_PARAMS = new SimpleLoggerConfiguration();
165
166 private static boolean initialized = false;
167
168 static void lazyInit() {
169 if (initialized) {
170 return;
171 }
172 initialized = true;
173 init();
174 }
175
176
177
178 static void init() {
179 CONFIG_PARAMS.init();
180 }
181
182
183 protected int currentLogLevel = LOG_LEVEL_INFO;
184
185 private transient String shortLogName = null;
186
187
188
189
190
191 public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
192
193 public static final String LOG_KEY_PREFIX = MavenBaseLogger.SYSTEM_PREFIX + "log.";
194
195 public static final String CACHE_OUTPUT_STREAM_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "cacheOutputStream";
196
197 public static final String WARN_LEVEL_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "warnLevelString";
198
199 public static final String LEVEL_IN_BRACKETS_KEY = MavenBaseLogger.SYSTEM_PREFIX + "levelInBrackets";
200
201 public static final String LOG_FILE_KEY = MavenBaseLogger.SYSTEM_PREFIX + "logFile";
202
203 public static final String SHOW_SHORT_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showShortLogName";
204
205 public static final String SHOW_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showLogName";
206
207 public static final String SHOW_THREAD_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadName";
208
209 public static final String SHOW_THREAD_ID_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadId";
210
211 public static final String DATE_TIME_FORMAT_KEY = MavenBaseLogger.SYSTEM_PREFIX + "dateTimeFormat";
212
213 public static final String SHOW_DATE_TIME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showDateTime";
214
215 public static final String DEFAULT_LOG_LEVEL_KEY = MavenBaseLogger.SYSTEM_PREFIX + "defaultLogLevel";
216
217
218
219
220
221 protected MavenBaseLogger(String name) {
222 this.name = name;
223
224 String levelString = recursivelyComputeLevelString();
225 if (levelString != null) {
226 this.currentLogLevel = SimpleLoggerConfiguration.stringToLevel(levelString);
227 } else {
228 this.currentLogLevel = CONFIG_PARAMS.defaultLogLevel;
229 }
230 }
231
232 String recursivelyComputeLevelString() {
233 String tempName = name;
234 String levelString = null;
235 int indexOfLastDot = tempName.length();
236 while ((levelString == null) && (indexOfLastDot > -1)) {
237 tempName = tempName.substring(0, indexOfLastDot);
238 levelString = CONFIG_PARAMS.getStringProperty(MavenBaseLogger.LOG_KEY_PREFIX + tempName, null);
239 indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
240 }
241 return levelString;
242 }
243
244
245
246
247
248
249
250
251 protected void write(StringBuilder buf, Throwable t) {
252 PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();
253
254 synchronized (CONFIG_PARAMS) {
255 targetStream.println(buf.toString());
256 writeThrowable(t, targetStream);
257 targetStream.flush();
258 }
259 }
260
261 protected void writeThrowable(Throwable t, PrintStream targetStream) {
262 if (t != null) {
263 t.printStackTrace(targetStream);
264 }
265 }
266
267 protected String getFormattedDate() {
268 Date now = new Date();
269 String dateText;
270 synchronized (CONFIG_PARAMS.dateFormatter) {
271 dateText = CONFIG_PARAMS.dateFormatter.format(now);
272 }
273 return dateText;
274 }
275
276 protected String computeShortName() {
277 return name.substring(name.lastIndexOf(".") + 1);
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 protected boolean isLevelEnabled(int logLevel) {
319
320
321 return (logLevel >= currentLogLevel);
322 }
323
324
325 public boolean isTraceEnabled() {
326 return isLevelEnabled(LOG_LEVEL_TRACE);
327 }
328
329
330 public boolean isDebugEnabled() {
331 return isLevelEnabled(LOG_LEVEL_DEBUG);
332 }
333
334
335 public boolean isInfoEnabled() {
336 return isLevelEnabled(LOG_LEVEL_INFO);
337 }
338
339
340 public boolean isWarnEnabled() {
341 return isLevelEnabled(LOG_LEVEL_WARN);
342 }
343
344
345 public boolean isErrorEnabled() {
346 return isLevelEnabled(LOG_LEVEL_ERROR);
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360 @Override
361 protected void handleNormalizedLoggingCall(
362 Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable) {
363
364 List<Marker> markers = null;
365
366 if (marker != null) {
367 markers = new ArrayList<>();
368 markers.add(marker);
369 }
370
371 innerHandleNormalizedLoggingCall(level, markers, messagePattern, arguments, throwable);
372 }
373
374 private void innerHandleNormalizedLoggingCall(
375 Level level, List<Marker> markers, String messagePattern, Object[] arguments, Throwable t) {
376
377 StringBuilder buf = new StringBuilder(32);
378
379
380 if (CONFIG_PARAMS.showDateTime) {
381 if (CONFIG_PARAMS.dateFormatter != null) {
382 buf.append(getFormattedDate());
383 buf.append(SP);
384 } else {
385 buf.append(System.currentTimeMillis() - START_TIME);
386 buf.append(SP);
387 }
388 }
389
390
391 if (CONFIG_PARAMS.showThreadName) {
392 buf.append('[');
393 buf.append(Thread.currentThread().getName());
394 buf.append("] ");
395 }
396
397 if (CONFIG_PARAMS.showThreadId) {
398 buf.append(TID_PREFIX);
399 buf.append(Thread.currentThread().getId());
400 buf.append(SP);
401 }
402
403 if (CONFIG_PARAMS.levelInBrackets) {
404 buf.append('[');
405 }
406
407
408 String levelStr = renderLevel(level.toInt());
409 buf.append(levelStr);
410 if (CONFIG_PARAMS.levelInBrackets) {
411 buf.append(']');
412 }
413 buf.append(SP);
414
415
416 if (CONFIG_PARAMS.showShortLogName) {
417 if (shortLogName == null) {
418 shortLogName = computeShortName();
419 }
420 buf.append(shortLogName).append(" - ");
421 } else if (CONFIG_PARAMS.showLogName) {
422 buf.append(name).append(" - ");
423 }
424
425 if (markers != null) {
426 buf.append(SP);
427 for (Marker marker : markers) {
428 buf.append(marker.getName()).append(SP);
429 }
430 }
431
432 String formattedMessage = MessageFormatter.basicArrayFormat(messagePattern, arguments);
433
434
435 buf.append(formattedMessage);
436
437 write(buf, t);
438 }
439
440 protected String renderLevel(int levelInt) {
441 switch (levelInt) {
442 case LOG_LEVEL_TRACE:
443 return "TRACE";
444 case LOG_LEVEL_DEBUG:
445 return ("DEBUG");
446 case LOG_LEVEL_INFO:
447 return "INFO";
448 case LOG_LEVEL_WARN:
449 return "WARN";
450 case LOG_LEVEL_ERROR:
451 return "ERROR";
452 default:
453 throw new IllegalStateException("Unrecognized level [" + levelInt + "]");
454 }
455 }
456
457 public void log(LoggingEvent event) {
458 int levelInt = event.getLevel().toInt();
459
460 if (!isLevelEnabled(levelInt)) {
461 return;
462 }
463
464 NormalizedParameters np = NormalizedParameters.normalize(event);
465
466 innerHandleNormalizedLoggingCall(
467 event.getLevel(), event.getMarkers(), np.getMessage(), np.getArguments(), event.getThrowable());
468 }
469
470 @Override
471 protected String getFullyQualifiedCallerName() {
472 return null;
473 }
474 }