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.level;
23  import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
24  
25  import java.io.PrintStream;
26  
27  /**
28   * Logger for Maven, that support colorization of levels and stacktraces.
29   * This class implements 2 methods introduced in slf4j-simple provider local copy.
30   * @since 3.5.0
31   */
32  public class MavenSimpleLogger
33      extends SimpleLogger
34  {
35      MavenSimpleLogger( String name )
36      {
37          super( name );
38      }
39  
40      @Override
41      protected String renderLevel( int level )
42      {
43          switch ( level )
44          {
45              case LOG_LEVEL_TRACE:
46                  return level().debug( "TRACE" ).toString();
47              case LOG_LEVEL_DEBUG:
48                  return level().debug( "DEBUG" ).toString();
49              case LOG_LEVEL_INFO:
50                  return level().info( "INFO" ).toString();
51              case LOG_LEVEL_WARN:
52                  return level().warning( "WARNING" ).toString();
53              case LOG_LEVEL_ERROR:
54              default:
55                  return level().error( "ERROR" ).toString();
56          }
57      }
58  
59      @Override
60      protected void writeThrowable( Throwable t, PrintStream stream )
61      {
62          if ( t == null )
63          {
64              return;
65          }
66          stream.print( buffer().failure( t.getClass().getName() ) );
67          if ( t.getMessage() != null )
68          {
69              stream.print( ": " );
70              stream.print( buffer().failure( t.getMessage() ) );
71          }
72          stream.println();
73  
74          while ( t != null )
75          {
76              for ( StackTraceElement e : t.getStackTrace() )
77              {
78                  stream.print( "    " );
79                  stream.print( buffer().strong( "at" ) );
80                  stream.print( " " + e.getClassName() + "." + e.getMethodName() );
81                  stream.print( buffer().a( " (" ).strong( getLocation( e ) ).a( ")" ) );
82                  stream.println();
83              }
84  
85              t = t.getCause();
86              if ( t != null )
87              {
88                  stream.print( buffer().strong( "Caused by" ).a( ": " ).a( t.getClass().getName() ) );
89                  if ( t.getMessage() != null )
90                  {
91                      stream.print( ": " );
92                      stream.print( buffer().failure( t.getMessage() ) );
93                  }
94                  stream.println();
95              }
96          }
97      }
98  
99      protected String getLocation( final StackTraceElement e )
100     {
101         assert e != null;
102 
103         if ( e.isNativeMethod() )
104         {
105             return "Native Method";
106         }
107         else if ( e.getFileName() == null )
108         {
109             return "Unknown Source";
110         }
111         else if ( e.getLineNumber() >= 0 )
112         {
113             return String.format( "%s:%s", e.getFileName(), e.getLineNumber() );
114         }
115         else
116         {
117             return e.getFileName();
118         }
119     }
120 }