View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Logger for Maven, that support colorization of levels and stacktraces.
28   * This class implements 2 methods introduced in slf4j-simple provider local copy.
29   * @since 3.5.0
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 && t != cause) {
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 }