View Javadoc

1   package org.apache.maven.doxia.logging;
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.PrintWriter;
23  import java.io.StringWriter;
24  
25  /**
26   * Logger with "standard" output and error output stream. The log prefix is voluntarily in lower case.
27   * <br/>
28   * Based on <code>org.apache.maven.plugin.logging.SystemStreamLog</code>.
29   *
30   * @author jdcasey
31   * @author ltheussl
32   * @version $Id: SystemStreamLog.java 733395 2009-01-10 23:09:40Z ltheussl $
33   * @since 1.1
34   */
35  public class SystemStreamLog
36      implements Log
37  {
38      private static final String EOL = System.getProperty( "line.separator" );
39  
40      private int currentLevel = LEVEL_INFO;
41  
42      /** {@inheritDoc} */
43      public void setLogLevel( int level )
44      {
45          if ( level <= LEVEL_DEBUG )
46          {
47              currentLevel = LEVEL_DEBUG;
48          }
49          else if ( level <= LEVEL_INFO )
50          {
51              currentLevel = LEVEL_INFO;
52          }
53          else if ( level <= LEVEL_WARN )
54          {
55              currentLevel = LEVEL_WARN;
56          }
57          else if ( level <= LEVEL_ERROR )
58          {
59              currentLevel = LEVEL_ERROR;
60          }
61          else
62          {
63              currentLevel = LEVEL_DISABLED;
64          }
65      }
66  
67      /** {@inheritDoc} */
68      public void debug( CharSequence content )
69      {
70          if ( isDebugEnabled() )
71          {
72              print( "debug", content );
73          }
74      }
75  
76      /** {@inheritDoc} */
77      public void debug( CharSequence content, Throwable error )
78      {
79          if ( isDebugEnabled() )
80          {
81              print( "debug", content, error );
82          }
83      }
84  
85      /** {@inheritDoc} */
86      public void debug( Throwable error )
87      {
88          if ( isDebugEnabled() )
89          {
90              print( "debug", error );
91          }
92      }
93  
94      /** {@inheritDoc} */
95      public void info( CharSequence content )
96      {
97          if ( isInfoEnabled() )
98          {
99              print( "info", content );
100         }
101     }
102 
103     /** {@inheritDoc} */
104     public void info( CharSequence content, Throwable error )
105     {
106         if ( isInfoEnabled() )
107         {
108             print( "info", content, error );
109         }
110     }
111 
112     /** {@inheritDoc} */
113     public void info( Throwable error )
114     {
115         if ( isInfoEnabled() )
116         {
117             print( "info", error );
118         }
119     }
120 
121     /** {@inheritDoc} */
122     public void warn( CharSequence content )
123     {
124         if ( isWarnEnabled() )
125         {
126             print( "warn", content );
127         }
128     }
129 
130     /** {@inheritDoc} */
131     public void warn( CharSequence content, Throwable error )
132     {
133         if ( isWarnEnabled() )
134         {
135             print( "warn", content, error );
136         }
137     }
138 
139     /** {@inheritDoc} */
140     public void warn( Throwable error )
141     {
142         if ( isWarnEnabled() )
143         {
144             print( "warn", error );
145         }
146     }
147 
148     /** {@inheritDoc} */
149     public void error( CharSequence content )
150     {
151         if ( isErrorEnabled() )
152         {
153             System.err.println( "[error] " + content.toString() );
154         }
155     }
156 
157     /** {@inheritDoc} */
158     public void error( CharSequence content, Throwable error )
159     {
160         if ( isErrorEnabled() )
161         {
162             StringWriter sWriter = new StringWriter();
163             PrintWriter pWriter = new PrintWriter( sWriter );
164 
165             error.printStackTrace( pWriter );
166 
167             System.err.println( "[error] " + content.toString()
168                 + EOL + EOL + sWriter.toString() );
169         }
170     }
171 
172     /** {@inheritDoc} */
173     public void error( Throwable error )
174     {
175         if ( isErrorEnabled() )
176         {
177             StringWriter sWriter = new StringWriter();
178             PrintWriter pWriter = new PrintWriter( sWriter );
179 
180             error.printStackTrace( pWriter );
181 
182             System.err.println( "[error] " + sWriter.toString() );
183         }
184     }
185 
186     /** {@inheritDoc} */
187     public boolean isDebugEnabled()
188     {
189         return ( currentLevel <= LEVEL_DEBUG );
190     }
191 
192     /** {@inheritDoc} */
193     public boolean isInfoEnabled()
194     {
195         return ( currentLevel <= LEVEL_INFO );
196     }
197 
198     /** {@inheritDoc} */
199     public boolean isWarnEnabled()
200     {
201         return ( currentLevel <= LEVEL_WARN );
202     }
203 
204     /** {@inheritDoc} */
205     public boolean isErrorEnabled()
206     {
207         return ( currentLevel <= LEVEL_ERROR );
208     }
209 
210       //
211      // private
212     //
213 
214     private void print( String prefix, CharSequence content )
215     {
216         System.out.println( "[" + prefix + "] " + content.toString() );
217     }
218 
219     private void print( String prefix, Throwable error )
220     {
221         StringWriter sWriter = new StringWriter();
222         PrintWriter pWriter = new PrintWriter( sWriter );
223 
224         error.printStackTrace( pWriter );
225 
226         System.out.println( "[" + prefix + "] " + sWriter.toString() );
227     }
228 
229     private void print( String prefix, CharSequence content, Throwable error )
230     {
231         StringWriter sWriter = new StringWriter();
232         PrintWriter pWriter = new PrintWriter( sWriter );
233 
234         error.printStackTrace( pWriter );
235 
236         System.out.println( "[" + prefix + "] " + content.toString()
237             + EOL + EOL + sWriter.toString() );
238     }
239 }