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 static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
22 import static org.apache.maven.shared.utils.logging.MessageUtils.level;
23
24 import java.io.PrintStream;
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 level().debug("TRACE");
42 case LOG_LEVEL_DEBUG:
43 return level().debug("DEBUG");
44 case LOG_LEVEL_INFO:
45 return level().info("INFO");
46 case LOG_LEVEL_WARN:
47 return level().warning("WARNING");
48 case LOG_LEVEL_ERROR:
49 default:
50 return level().error("ERROR");
51 }
52 }
53
54 @Override
55 protected void writeThrowable(Throwable t, PrintStream stream) {
56 if (t == null) {
57 return;
58 }
59 stream.print(buffer().failure(t.getClass().getName()));
60 if (t.getMessage() != null) {
61 stream.print(": ");
62 stream.print(buffer().failure(t.getMessage()));
63 }
64 stream.println();
65
66 while (t != null) {
67 for (StackTraceElement e : t.getStackTrace()) {
68 stream.print(" ");
69 stream.print(buffer().strong("at"));
70 stream.print(" " + e.getClassName() + "." + e.getMethodName());
71 stream.print(buffer().a(" (").strong(getLocation(e)).a(")"));
72 stream.println();
73 }
74
75 t = t.getCause();
76 if (t != null) {
77 stream.print(buffer().strong("Caused by").a(": ").a(t.getClass().getName()));
78 if (t.getMessage() != null) {
79 stream.print(": ");
80 stream.print(buffer().failure(t.getMessage()));
81 }
82 stream.println();
83 }
84 }
85 }
86
87 protected String getLocation(final StackTraceElement e) {
88 assert e != null;
89
90 if (e.isNativeMethod()) {
91 return "Native Method";
92 } else if (e.getFileName() == null) {
93 return "Unknown Source";
94 } else if (e.getLineNumber() >= 0) {
95 return String.format("%s:%s", e.getFileName(), e.getLineNumber());
96 } else {
97 return e.getFileName();
98 }
99 }
100 }