View Javadoc
1   package org.apache.maven.shared.invoker;
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 java.io.PrintStream;
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  
26  /**
27   * Offers a logger that writes to a print stream like {@link System#out}.
28   * 
29   * @version $Id: PrintStreamLogger.java 1635406 2014-10-30 06:51:13Z hboutemy $
30   * @since 2.0.9
31   */
32  public class PrintStreamLogger
33      implements InvokerLogger
34  {
35  
36      /**
37       * The print stream to write to, never <code>null</code>.
38       */
39      private PrintStream out;
40  
41      /**
42       * The threshold used to filter messages.
43       */
44      private int threshold;
45  
46      /**
47       * Creates a new logger that writes to {@link System#out} and has a threshold of {@link #INFO}.
48       */
49      public PrintStreamLogger()
50      {
51          this( System.out, INFO );
52      }
53  
54      /**
55       * Creates a new logger that writes to the specified print stream.
56       * 
57       * @param out The print stream to write to, must not be <code>null</code>.
58       * @param threshold The threshold for the logger.
59       */
60      public PrintStreamLogger( PrintStream out, int threshold )
61      {
62          if ( out == null )
63          {
64              throw new NullPointerException( "missing output stream" );
65          }
66          this.out = out;
67          setThreshold( threshold );
68      }
69  
70      /**
71       * Writes the specified message and exception to the print stream.
72       * 
73       * @param level The priority level of the message.
74       * @param message The message to log, may be <code>null</code>.
75       * @param error The exception to log, may be <code>null</code>.
76       */
77      private void log( int level, String message, Throwable error )
78      {
79          if ( level > threshold )
80          {
81              // don't log when it doesn't match your threshold.
82              return;
83          }
84  
85          if ( message == null && error == null )
86          {
87              // don't log when there's nothing to log.
88              return;
89          }
90  
91          StringBuffer buffer = new StringBuffer();
92  
93          switch ( level )
94          {
95              case ( DEBUG ):
96                  buffer.append( "[DEBUG]" );
97                  break;
98  
99              case ( INFO ):
100                 buffer.append( "[INFO]" );
101                 break;
102 
103             case ( WARN ):
104                 buffer.append( "[WARN]" );
105                 break;
106 
107             case ( ERROR ):
108                 buffer.append( "[ERROR]" );
109                 break;
110 
111             case ( FATAL ):
112                 buffer.append( "[FATAL]" );
113                 break;
114 
115             default:
116         }
117 
118         buffer.append( ' ' );
119 
120         if ( message != null )
121         {
122             buffer.append( message );
123         }
124 
125         if ( error != null )
126         {
127             StringWriter writer = new StringWriter();
128             PrintWriter pWriter = new PrintWriter( writer );
129 
130             error.printStackTrace( pWriter );
131 
132             if ( message != null )
133             {
134                 buffer.append( '\n' );
135             }
136 
137             buffer.append( "Error:\n" );
138             buffer.append( writer.toString() );
139         }
140 
141         out.println( buffer.toString() );
142     }
143 
144     public void debug( String message )
145     {
146         log( DEBUG, message, null );
147     }
148 
149     public void debug( String message, Throwable throwable )
150     {
151         log( DEBUG, message, throwable );
152     }
153 
154     public void info( String message )
155     {
156         log( INFO, message, null );
157     }
158 
159     public void info( String message, Throwable throwable )
160     {
161         log( INFO, message, throwable );
162     }
163 
164     public void warn( String message )
165     {
166         log( WARN, message, null );
167     }
168 
169     public void warn( String message, Throwable throwable )
170     {
171         log( WARN, message, throwable );
172     }
173 
174     public void error( String message )
175     {
176         log( ERROR, message, null );
177     }
178 
179     public void error( String message, Throwable throwable )
180     {
181         log( ERROR, message, throwable );
182     }
183 
184     public void fatalError( String message )
185     {
186         log( FATAL, message, null );
187     }
188 
189     public void fatalError( String message, Throwable throwable )
190     {
191         log( FATAL, message, throwable );
192     }
193 
194     public boolean isDebugEnabled()
195     {
196         return threshold >= DEBUG;
197     }
198 
199     public boolean isErrorEnabled()
200     {
201         return threshold >= ERROR;
202     }
203 
204     public boolean isFatalErrorEnabled()
205     {
206         return threshold >= FATAL;
207     }
208 
209     public boolean isInfoEnabled()
210     {
211         return threshold >= INFO;
212     }
213 
214     public boolean isWarnEnabled()
215     {
216         return threshold >= WARN;
217     }
218 
219     public int getThreshold()
220     {
221         return threshold;
222     }
223 
224     public void setThreshold( int threshold )
225     {
226         this.threshold = threshold;
227     }
228 
229 }