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
25 import static org.apache.maven.cli.jansi.MessageUtils.builder;
26
27
28
29
30
31
32
33 public class MavenSimpleLogger extends SimpleLogger {
34
35 private final String traceRenderedLevel = builder().trace("TRACE").build();
36 private final String debugRenderedLevel = builder().debug("DEBUG").build();
37 private final String infoRenderedLevel = builder().info("INFO").build();
38 private final String warnRenderedLevel = builder().warning("WARNING").build();
39 private final String errorRenderedLevel = builder().error("ERROR").build();
40
41 MavenSimpleLogger(String name) {
42 super(name);
43 }
44
45 @Override
46 protected String renderLevel(int level) {
47 switch (level) {
48 case LOG_LEVEL_TRACE:
49 return traceRenderedLevel;
50 case LOG_LEVEL_DEBUG:
51 return debugRenderedLevel;
52 case LOG_LEVEL_INFO:
53 return infoRenderedLevel;
54 case LOG_LEVEL_WARN:
55 return warnRenderedLevel;
56 case LOG_LEVEL_ERROR:
57 default:
58 return errorRenderedLevel;
59 }
60 }
61
62 @Override
63 protected void writeThrowable(Throwable t, PrintStream stream) {
64 if (t == null) {
65 return;
66 }
67 MessageBuilder builder = builder().failure(t.getClass().getName());
68 if (t.getMessage() != null) {
69 builder.a(": ").failure(t.getMessage());
70 }
71 stream.println(builder);
72
73 printStackTrace(t, stream, "");
74 }
75
76 private void printStackTrace(Throwable t, PrintStream stream, String prefix) {
77 MessageBuilder builder = builder();
78 for (StackTraceElement e : t.getStackTrace()) {
79 builder.a(prefix);
80 builder.a(" ");
81 builder.strong("at");
82 builder.a(" ");
83 builder.a(e.getClassName());
84 builder.a(".");
85 builder.a(e.getMethodName());
86 builder.a(" (");
87 builder.strong(getLocation(e));
88 builder.a(")");
89 stream.println(builder);
90 builder.setLength(0);
91 }
92 for (Throwable se : t.getSuppressed()) {
93 writeThrowable(se, stream, "Suppressed", prefix + " ");
94 }
95 Throwable cause = t.getCause();
96 if (cause != null && t != cause) {
97 writeThrowable(cause, stream, "Caused by", prefix);
98 }
99 }
100
101 private void writeThrowable(Throwable t, PrintStream stream, String caption, String prefix) {
102 MessageBuilder builder =
103 builder().a(prefix).strong(caption).a(": ").a(t.getClass().getName());
104 if (t.getMessage() != null) {
105 builder.a(": ").failure(t.getMessage());
106 }
107 stream.println(builder);
108
109 printStackTrace(t, stream, prefix);
110 }
111
112 protected String getLocation(final StackTraceElement e) {
113 assert e != null;
114
115 if (e.isNativeMethod()) {
116 return "Native Method";
117 } else if (e.getFileName() == null) {
118 return "Unknown Source";
119 } else if (e.getLineNumber() >= 0) {
120 return e.getFileName() + ":" + e.getLineNumber();
121 } else {
122 return e.getFileName();
123 }
124 }
125 }