View Javadoc

1   package org.codehaus.mojo.tomcat.log;
2   
3   /*
4    * Copyright 2006 Mark Hobson.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogConfigurationException;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * A JCL log implementation that delegates to a Maven log.
25   * 
26   * @author Mark Hobson <markhobson@gmail.com>
27   * @version $Id: MavenLog.java 6588 2008-03-28 12:22:57Z bentmann $
28   */
29  public class MavenLog implements Log
30  {
31      // ----------------------------------------------------------------------
32      // Constants
33      // ----------------------------------------------------------------------
34  
35      /**
36       * The name of the log factory attribute that holds the Maven log to use.
37       */
38      private static final String MAVEN_LOG_ATTRIBUTE = "maven.log";
39  
40      // ----------------------------------------------------------------------
41      // Fields
42      // ----------------------------------------------------------------------
43  
44      /**
45       * The log factory to read attributes from.
46       */
47      private final LogFactory logFactory;
48  
49      // ----------------------------------------------------------------------
50      // Constructors
51      // ----------------------------------------------------------------------
52  
53      /**
54       * Creates a new <code>MavenLog</code> from the specified log factory.
55       * 
56       * @param logFactory
57       *            the log factory to read attributes from
58       * @throws IllegalArgumentException
59       *             if the specified log factory was null
60       */
61      public MavenLog( LogFactory logFactory )
62      {
63          if ( logFactory == null )
64          {
65              throw new IllegalArgumentException( "Log factory cannot be null" );
66          }
67  
68          this.logFactory = logFactory;
69      }
70  
71      // ----------------------------------------------------------------------
72      // Log Implementation
73      // ----------------------------------------------------------------------
74  
75      /*
76       * @see org.apache.commons.logging.Log#isTraceEnabled()
77       */
78      public boolean isTraceEnabled()
79      {
80          return isDebugEnabled();
81      }
82  
83      /*
84       * @see org.apache.commons.logging.Log#isDebugEnabled()
85       */
86      public boolean isDebugEnabled()
87      {
88          return getMavenLog().isDebugEnabled();
89      }
90  
91      /*
92       * @see org.apache.commons.logging.Log#isInfoEnabled()
93       */
94      public boolean isInfoEnabled()
95      {
96          return getMavenLog().isInfoEnabled();
97      }
98  
99      /*
100      * @see org.apache.commons.logging.Log#isWarnEnabled()
101      */
102     public boolean isWarnEnabled()
103     {
104         return getMavenLog().isWarnEnabled();
105     }
106 
107     /*
108      * @see org.apache.commons.logging.Log#isErrorEnabled()
109      */
110     public boolean isErrorEnabled()
111     {
112         return getMavenLog().isErrorEnabled();
113     }
114 
115     /*
116      * @see org.apache.commons.logging.Log#isFatalEnabled()
117      */
118     public boolean isFatalEnabled()
119     {
120         return isErrorEnabled();
121     }
122 
123     /*
124      * @see org.apache.commons.logging.Log#trace(java.lang.Object)
125      */
126     public void trace( Object message )
127     {
128         if ( isTraceEnabled() )
129         {
130             debug( message );
131         }
132     }
133 
134     /*
135      * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
136      */
137     public void trace( Object message, Throwable throwable )
138     {
139         if ( isTraceEnabled() )
140         {
141             debug( message, throwable );
142         }
143     }
144 
145     /*
146      * @see org.apache.commons.logging.Log#debug(java.lang.Object)
147      */
148     public void debug( Object message )
149     {
150         if ( isDebugEnabled() )
151         {
152             getMavenLog().debug( String.valueOf( message ) );
153         }
154     }
155 
156     /*
157      * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
158      */
159     public void debug( Object message, Throwable throwable )
160     {
161         if ( isDebugEnabled() )
162         {
163             getMavenLog().debug( String.valueOf( message ), throwable );
164         }
165     }
166 
167     /*
168      * @see org.apache.commons.logging.Log#info(java.lang.Object)
169      */
170     public void info( Object message )
171     {
172         if ( isInfoEnabled() )
173         {
174             getMavenLog().info( String.valueOf( message ) );
175         }
176     }
177 
178     /*
179      * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
180      */
181     public void info( Object message, Throwable throwable )
182     {
183         if ( isInfoEnabled() )
184         {
185             getMavenLog().info( String.valueOf( message ), throwable );
186         }
187     }
188 
189     /*
190      * @see org.apache.commons.logging.Log#warn(java.lang.Object)
191      */
192     public void warn( Object message )
193     {
194         if ( isWarnEnabled() )
195         {
196             getMavenLog().warn( String.valueOf( message ) );
197         }
198     }
199 
200     /*
201      * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
202      */
203     public void warn( Object message, Throwable throwable )
204     {
205         if ( isWarnEnabled() )
206         {
207             getMavenLog().warn( String.valueOf( message ), throwable );
208         }
209     }
210 
211     /*
212      * @see org.apache.commons.logging.Log#error(java.lang.Object)
213      */
214     public void error( Object message )
215     {
216         if ( isErrorEnabled() )
217         {
218             getMavenLog().error( String.valueOf( message ) );
219         }
220     }
221 
222     /*
223      * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
224      */
225     public void error( Object message, Throwable throwable )
226     {
227         if ( isErrorEnabled() )
228         {
229             getMavenLog().error( String.valueOf( message ), throwable );
230         }
231     }
232 
233     /*
234      * @see org.apache.commons.logging.Log#fatal(java.lang.Object)
235      */
236     public void fatal( Object message )
237     {
238         if ( isFatalEnabled() )
239         {
240             error( message );
241         }
242     }
243 
244     /*
245      * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
246      */
247     public void fatal( Object message, Throwable throwable )
248     {
249         if ( isFatalEnabled() )
250         {
251             error( message, throwable );
252         }
253     }
254 
255     // ----------------------------------------------------------------------
256     // Protected Methods
257     // ----------------------------------------------------------------------
258 
259     /**
260      * Gets the underlying Maven log to delegate to.
261      * 
262      * @return the Maven log
263      */
264     protected org.apache.maven.plugin.logging.Log getMavenLog()
265     {
266         org.apache.maven.plugin.logging.Log mavenLog =
267             (org.apache.maven.plugin.logging.Log) logFactory.getAttribute( MAVEN_LOG_ATTRIBUTE );
268 
269         if ( mavenLog == null )
270         {
271             throw new LogConfigurationException( "The LogFactory attribute " + MAVEN_LOG_ATTRIBUTE + " must be set." );
272         }
273 
274         return mavenLog;
275     }
276 }