1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.slf4j.impl;
20
21 import java.io.PrintStream;
22
23 import org.apache.maven.api.services.MessageBuilder;
24 import org.apache.maven.cli.jansi.MessageUtils;
25
26
27
28
29
30
31
32 public class MavenSimpleLogger extends SimpleLogger {
33 MavenSimpleLogger(String name) {
34 super(name);
35 }
36
37 @Override
38 protected String renderLevel(int level) {
39 switch (level) {
40 case LOG_LEVEL_TRACE:
41 return builder().debug("TRACE").build();
42 case LOG_LEVEL_DEBUG:
43 return builder().debug("DEBUG").build();
44 case LOG_LEVEL_INFO:
45 return builder().info("INFO").build();
46 case LOG_LEVEL_WARN:
47 return builder().warning("WARNING").build();
48 case LOG_LEVEL_ERROR:
49 default:
50 return builder().error("ERROR").build();
51 }
52 }
53
54 @Override
55 protected void writeThrowable(Throwable t, PrintStream stream) {
56 if (t == null) {
57 return;
58 }
59 stream.print(builder().failure(t.getClass().getName()));
60 if (t.getMessage() != null) {
61 stream.print(": ");
62 stream.print(builder().failure(t.getMessage()));
63 }
64 stream.println();
65
66 printStackTrace(t, stream, "");
67 }
68
69 private void printStackTrace(Throwable t, PrintStream stream, String prefix) {
70 for (StackTraceElement e : t.getStackTrace()) {
71 stream.print(prefix);
72 stream.print(" ");
73 stream.print(builder().strong("at"));
74 stream.print(" " + e.getClassName() + "." + e.getMethodName());
75 stream.print(builder().a(" (").strong(getLocation(e)).a(")"));
76 stream.println();
77 }
78 for (Throwable se : t.getSuppressed()) {
79 writeThrowable(se, stream, "Suppressed", prefix + " ");
80 }
81 Throwable cause = t.getCause();
82 if (cause != null) {
83 writeThrowable(cause, stream, "Caused by", prefix);
84 }
85 }
86
87 private void writeThrowable(Throwable t, PrintStream stream, String caption, String prefix) {
88 stream.print(builder().a(prefix).strong(caption).a(": ").a(t.getClass().getName()));
89 if (t.getMessage() != null) {
90 stream.print(": ");
91 stream.print(builder().failure(t.getMessage()));
92 }
93 stream.println();
94
95 printStackTrace(t, stream, prefix);
96 }
97
98 protected String getLocation(final StackTraceElement e) {
99 assert e != null;
100
101 if (e.isNativeMethod()) {
102 return "Native Method";
103 } else if (e.getFileName() == null) {
104 return "Unknown Source";
105 } else if (e.getLineNumber() >= 0) {
106 return String.format("%s:%s", e.getFileName(), e.getLineNumber());
107 } else {
108 return e.getFileName();
109 }
110 }
111
112 private MessageBuilder builder() {
113 return MessageUtils.builder();
114 }
115 }