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          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  }