View Javadoc
1   package org.slf4j.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
23  
24  import java.io.PrintStream;
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
32      extends SimpleLogger
33  {
34      MavenSimpleLogger( String name )
35      {
36          super( name );
37      }
38  
39      @Override
40      protected String renderLevel( int level )
41      {
42          switch ( level )
43          {
44              case LOG_LEVEL_TRACE:
45                  return buffer().debug( "TRACE" ).toString();
46              case LOG_LEVEL_DEBUG:
47                  return buffer().debug( "DEBUG" ).toString();
48              case LOG_LEVEL_INFO:
49                  return buffer().info( "INFO" ).toString();
50              case LOG_LEVEL_WARN:
51                  return buffer().warning( "WARNING" ).toString();
52              case LOG_LEVEL_ERROR:
53              default:
54                  return buffer().error( "ERROR" ).toString();
55          }
56      }
57  
58      @Override
59      protected void renderThrowable( Throwable t, PrintStream stream )
60      {
61          stream.print( buffer().failure( t.getClass().getName() ) );
62          if ( t.getMessage() != null )
63          {
64              stream.print( ": " );
65              stream.print( buffer().failure( t.getMessage() ) );
66          }
67          stream.println();
68  
69          while ( t != null )
70          {
71              for ( StackTraceElement e : t.getStackTrace() )
72              {
73                  stream.print( "    " );
74                  stream.print( buffer().strong( "at" ) );
75                  stream.print( " " + e.getClassName() + "." + e.getMethodName() );
76                  stream.print( buffer().a( " (" ).strong( getLocation( e ) ).a( ")" ) );
77                  stream.println();
78              }
79  
80              t = t.getCause();
81              if ( t != null )
82              {
83                  stream.print( buffer().strong( "Caused by" ).a( ": " ).a( t.getClass().getName() ) );
84                  if ( t.getMessage() != null )
85                  {
86                      stream.print( ": " );
87                      stream.print( buffer().failure( t.getMessage() ) );
88                  }
89                  stream.println();
90              }
91          }
92      }
93  
94      protected String getLocation( final StackTraceElement e )
95      {
96          assert e != null;
97  
98          if ( e.isNativeMethod() )
99          {
100             return "Native Method";
101         }
102         else if ( e.getFileName() == null )
103         {
104             return "Unknown Source";
105         }
106         else if ( e.getLineNumber() >= 0 )
107         {
108             return String.format( "%s:%s", e.getFileName(), e.getLineNumber() );
109         }
110         else
111         {
112             return e.getFileName();
113         }
114     }
115 }