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