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 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   * Logger for Maven, that support colorization of levels and stacktraces. This class implements 2 methods introduced in
28   * slf4j-simple provider local copy.
29   *
30   * @since 3.5.0
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 }