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 writeThrowable( Throwable t, PrintStream stream )
60      {
61          if ( t == null )
62          {
63              return;
64          }
65          stream.print( buffer().failure( t.getClass().getName() ) );
66          if ( t.getMessage() != null )
67          {
68              stream.print( ": " );
69              stream.print( buffer().failure( t.getMessage() ) );
70          }
71          stream.println();
72  
73          while ( t != null )
74          {
75              for ( StackTraceElement e : t.getStackTrace() )
76              {
77                  stream.print( "    " );
78                  stream.print( buffer().strong( "at" ) );
79                  stream.print( " " + e.getClassName() + "." + e.getMethodName() );
80                  stream.print( buffer().a( " (" ).strong( getLocation( e ) ).a( ")" ) );
81                  stream.println();
82              }
83  
84              t = t.getCause();
85              if ( t != null )
86              {
87                  stream.print( buffer().strong( "Caused by" ).a( ": " ).a( t.getClass().getName() ) );
88                  if ( t.getMessage() != null )
89                  {
90                      stream.print( ": " );
91                      stream.print( buffer().failure( t.getMessage() ) );
92                  }
93                  stream.println();
94              }
95          }
96      }
97  
98      protected String getLocation( final StackTraceElement e )
99      {
100         assert e != null;
101 
102         if ( e.isNativeMethod() )
103         {
104             return "Native Method";
105         }
106         else if ( e.getFileName() == null )
107         {
108             return "Unknown Source";
109         }
110         else if ( e.getLineNumber() >= 0 )
111         {
112             return String.format( "%s:%s", e.getFileName(), e.getLineNumber() );
113         }
114         else
115         {
116             return e.getFileName();
117         }
118     }
119 }