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.time.Clock;
23 import java.time.Duration;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.maven.api.MonotonicClock;
29 import org.slf4j.Logger;
30 import org.slf4j.Marker;
31 import org.slf4j.event.Level;
32 import org.slf4j.event.LoggingEvent;
33 import org.slf4j.helpers.LegacyAbstractLogger;
34 import org.slf4j.helpers.MessageFormatter;
35 import org.slf4j.helpers.NormalizedParameters;
36 import org.slf4j.spi.LocationAwareLogger;
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
145
146
147 public class MavenBaseLogger extends LegacyAbstractLogger {
148
149 private static final long serialVersionUID = -632788891211436180L;
150
151 private static final Clock MONOTONIC_CLOCK = Clock.tick(Clock.systemUTC(), Duration.ofMillis(1));
152 private static final Instant START_TIME = MonotonicClock.now();
153
154 protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
155 protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
156 protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
157 protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
158 protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
159
160 static final char SP = ' ';
161 static final String TID_PREFIX = "tid=";
162
163
164
165
166 protected static final int LOG_LEVEL_OFF = LOG_LEVEL_ERROR + 10;
167
168 static final SimpleLoggerConfiguration CONFIG_PARAMS = new SimpleLoggerConfiguration();
169
170 private static boolean initialized = false;
171
172 static void lazyInit() {
173 if (initialized) {
174 return;
175 }
176 initialized = true;
177 init();
178 }
179
180
181
182 static void init() {
183 CONFIG_PARAMS.init();
184 }
185
186
187 protected int currentLogLevel = LOG_LEVEL_INFO;
188
189 private transient String shortLogName = null;
190
191
192
193
194
195 public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
196
197 public static final String LOG_KEY_PREFIX = MavenBaseLogger.SYSTEM_PREFIX + "log.";
198
199 public static final String CACHE_OUTPUT_STREAM_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "cacheOutputStream";
200
201 public static final String WARN_LEVEL_STRING_KEY = MavenBaseLogger.SYSTEM_PREFIX + "warnLevelString";
202
203 public static final String LEVEL_IN_BRACKETS_KEY = MavenBaseLogger.SYSTEM_PREFIX + "levelInBrackets";
204
205 public static final String LOG_FILE_KEY = MavenBaseLogger.SYSTEM_PREFIX + "logFile";
206
207 public static final String SHOW_SHORT_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showShortLogName";
208
209 public static final String SHOW_LOG_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showLogName";
210
211 public static final String SHOW_THREAD_NAME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadName";
212
213 public static final String SHOW_THREAD_ID_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showThreadId";
214
215 public static final String DATE_TIME_FORMAT_KEY = MavenBaseLogger.SYSTEM_PREFIX + "dateTimeFormat";
216
217 public static final String SHOW_DATE_TIME_KEY = MavenBaseLogger.SYSTEM_PREFIX + "showDateTime";
218
219 public static final String DEFAULT_LOG_LEVEL_KEY = MavenBaseLogger.SYSTEM_PREFIX + "defaultLogLevel";
220
221
222
223
224
225 protected MavenBaseLogger(String name) {
226 this.name = name;
227
228 String levelString = recursivelyComputeLevelString();
229 if (levelString != null) {
230 this.currentLogLevel = SimpleLoggerConfiguration.stringToLevel(levelString);
231 } else {
232 this.currentLogLevel = CONFIG_PARAMS.defaultLogLevel;
233 }
234 }
235
236 String recursivelyComputeLevelString() {
237 String tempName = name;
238 String levelString = null;
239 int indexOfLastDot = tempName.length();
240 while ((levelString == null) && (indexOfLastDot > -1)) {
241 tempName = tempName.substring(0, indexOfLastDot);
242 levelString = CONFIG_PARAMS.getStringProperty(MavenBaseLogger.LOG_KEY_PREFIX + tempName, null);
243 indexOfLastDot = String.valueOf(tempName).lastIndexOf(".");
244 }
245 return levelString;
246 }
247
248
249
250
251
252
253
254
255 protected void write(StringBuilder buf, Throwable t) {
256 PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();
257
258 synchronized (CONFIG_PARAMS) {
259 targetStream.println(buf.toString());
260 writeThrowable(t, targetStream);
261 targetStream.flush();
262 }
263 }
264
265 protected void writeThrowable(Throwable t, PrintStream targetStream) {
266 if (t != null) {
267 t.printStackTrace(targetStream);
268 }
269 }
270
271 protected String getFormattedDate() {
272 Instant now = MonotonicClock.now();
273 String dateText;
274 synchronized (CONFIG_PARAMS.dateFormatter) {
275 dateText = CONFIG_PARAMS.dateFormatter.format(now);
276 }
277 return dateText;
278 }
279
280 protected String computeShortName() {
281 return name.substring(name.lastIndexOf(".") + 1);
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
319
320
321
322 protected boolean isLevelEnabled(int logLevel) {
323
324
325 return (logLevel >= currentLogLevel);
326 }
327
328
329 public boolean isTraceEnabled() {
330 return isLevelEnabled(LOG_LEVEL_TRACE);
331 }
332
333
334 public boolean isDebugEnabled() {
335 return isLevelEnabled(LOG_LEVEL_DEBUG);
336 }
337
338
339 public boolean isInfoEnabled() {
340 return isLevelEnabled(LOG_LEVEL_INFO);
341 }
342
343
344 public boolean isWarnEnabled() {
345 return isLevelEnabled(LOG_LEVEL_WARN);
346 }
347
348
349 public boolean isErrorEnabled() {
350 return isLevelEnabled(LOG_LEVEL_ERROR);
351 }
352
353
354
355
356
357
358
359
360
361
362
363
364 @Override
365 protected void handleNormalizedLoggingCall(
366 Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable) {
367
368 List<Marker> markers = null;
369
370 if (marker != null) {
371 markers = new ArrayList<>();
372 markers.add(marker);
373 }
374
375 innerHandleNormalizedLoggingCall(level, markers, messagePattern, arguments, throwable);
376 }
377
378 private void innerHandleNormalizedLoggingCall(
379 Level level, List<Marker> markers, String messagePattern, Object[] arguments, Throwable t) {
380
381 StringBuilder buf = new StringBuilder(32);
382
383
384 if (CONFIG_PARAMS.showDateTime) {
385 if (CONFIG_PARAMS.dateFormatter != null) {
386 buf.append(getFormattedDate());
387 buf.append(SP);
388 } else {
389 buf.append(Duration.between(START_TIME, MonotonicClock.now()).toMillis());
390 buf.append(SP);
391 }
392 }
393
394
395 if (CONFIG_PARAMS.showThreadName) {
396 buf.append('[');
397 buf.append(Thread.currentThread().getName());
398 buf.append("] ");
399 }
400
401 if (CONFIG_PARAMS.showThreadId) {
402 buf.append(TID_PREFIX);
403 buf.append(Thread.currentThread().getId());
404 buf.append(SP);
405 }
406
407 if (CONFIG_PARAMS.levelInBrackets) {
408 buf.append('[');
409 }
410
411
412 String levelStr = renderLevel(level.toInt());
413 buf.append(levelStr);
414 if (CONFIG_PARAMS.levelInBrackets) {
415 buf.append(']');
416 }
417 buf.append(SP);
418
419
420 if (CONFIG_PARAMS.showShortLogName) {
421 if (shortLogName == null) {
422 shortLogName = computeShortName();
423 }
424 buf.append(shortLogName).append(" - ");
425 } else if (CONFIG_PARAMS.showLogName) {
426 buf.append(name).append(" - ");
427 }
428
429 if (markers != null) {
430 buf.append(SP);
431 for (Marker marker : markers) {
432 buf.append(marker.getName()).append(SP);
433 }
434 }
435
436 String formattedMessage = MessageFormatter.basicArrayFormat(messagePattern, arguments);
437
438
439 buf.append(formattedMessage);
440
441 write(buf, t);
442 }
443
444 protected String renderLevel(int levelInt) {
445 switch (levelInt) {
446 case LOG_LEVEL_TRACE:
447 return "TRACE";
448 case LOG_LEVEL_DEBUG:
449 return ("DEBUG");
450 case LOG_LEVEL_INFO:
451 return "INFO";
452 case LOG_LEVEL_WARN:
453 return "WARN";
454 case LOG_LEVEL_ERROR:
455 return "ERROR";
456 default:
457 throw new IllegalStateException("Unrecognized level [" + levelInt + "]");
458 }
459 }
460
461 public void log(LoggingEvent event) {
462 int levelInt = event.getLevel().toInt();
463
464 if (!isLevelEnabled(levelInt)) {
465 return;
466 }
467
468 NormalizedParameters np = NormalizedParameters.normalize(event);
469
470 innerHandleNormalizedLoggingCall(
471 event.getLevel(), event.getMarkers(), np.getMessage(), np.getArguments(), event.getThrowable());
472 }
473
474 @Override
475 protected String getFullyQualifiedCallerName() {
476 return null;
477 }
478 }