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 while (t != null) {
66 for (StackTraceElement e : t.getStackTrace()) {
67 stream.print(" ");
68 stream.print(buffer().strong("at"));
69 stream.print(" " + e.getClassName() + "." + e.getMethodName());
70 stream.print(buffer().a(" (").strong(getLocation(e)).a(")"));
71 stream.println();
72 }
73
74 t = t.getCause();
75 if (t != null) {
76 stream.print(buffer().strong("Caused by").a(": ").a(t.getClass().getName()));
77 if (t.getMessage() != null) {
78 stream.print(": ");
79 stream.print(buffer().failure(t.getMessage()));
80 }
81 stream.println();
82 }
83 }
84 }
85
86 protected String getLocation(final StackTraceElement e) {
87 assert e != null;
88
89 if (e.isNativeMethod()) {
90 return "Native Method";
91 } else if (e.getFileName() == null) {
92 return "Unknown Source";
93 } else if (e.getLineNumber() >= 0) {
94 return String.format("%s:%s", e.getFileName(), e.getLineNumber());
95 } else {
96 return e.getFileName();
97 }
98 }
99 }