View Javadoc

1   package org.apache.dvsl;
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 org.apache.tools.ant.Project;
23  import org.apache.tools.ant.Task;
24  
25  import org.apache.velocity.runtime.RuntimeServices;
26  
27  import org.apache.velocity.runtime.log.LogChute;
28  
29  /**
30   * Implementation of a logger to output messages via an Ant Task's log
31   * method.  Velocity log levels are mapped to corresponding log levels
32   * defined in Ant's logging API.  The end result is messages will only
33   * be output if Ant log level is high enough.
34   *
35   * @author    <a href="mailto:billb@progress.com">Bill Burton</a>
36   * @version   $Id:$
37   */
38  public class AntLogChute implements LogChute
39  {
40  
41      // Reference to the Ant Task object that initialized the Velocity Engine.
42      Task task;
43  
44      /**
45       * Initialize this logger with a reference to the calling Ant Task
46       *
47       * @param task Ant Task to use for logging.  This must not be null.
48       */
49      public AntLogChute(Task task)
50      {
51          this.task = task;
52      }
53  
54      /**
55       * Initialize the logger.
56       *
57       * @throws Exception if null was passed into the constructor
58       */
59      public void init( RuntimeServices rs ) throws Exception
60      {
61          if ( task == null )
62          {
63              throw new Exception( "PANIC: " + this.getClass().getName() +
64                      " was instantiated with a null Ant Task reference");
65          }
66      }
67  
68      /**
69       * <p>
70       * Log Velocity messages through the Ant Task log method.  The mapping of logging
71       * levels from Velocity to Ant is as follows:
72       * </p>
73       *
74       * <blockquote><pre>
75       * Velocity Level      --&gt;  Ant Level
76       * LogSystem.TRACE_ID  --&gt;  Project.MSG_DEBUG
77       * LogSystem.DEBUG_ID  --&gt;  Project.MSG_DEBUG
78       * LogSystem.INFO_ID   --&gt;  Project.MSG_VERBOSE
79       * LogSystem.WARN_ID   --&gt;  Project.MSG_WARN
80       * LogSystem.ERROR_ID  --&gt;  Project.MSG_ERR
81       * </pre></blockquote>
82       *
83       * @param level    severity level
84       * @param message  complete error message
85       * @see   org.apache.velocity.runtime.log.LogChute
86       * @see   org.apache.tools.ant.Task#log(String, int)
87       */
88      public void log(int level, String message) {
89          switch ( level )
90          {
91              case LogChute.TRACE_ID:
92                  task.log( LogChute.TRACE_PREFIX + message, Project.MSG_DEBUG);
93                  break;
94              case LogChute.DEBUG_ID:
95                  task.log( LogChute.DEBUG_PREFIX + message, Project.MSG_DEBUG );
96                  break;
97              case LogChute.INFO_ID:
98                  task.log( LogChute.INFO_PREFIX + message, Project.MSG_VERBOSE );
99                  break;
100             case LogChute.WARN_ID:
101                 task.log( LogChute.WARN_PREFIX + message, Project.MSG_WARN );
102                 break;
103             case LogChute.ERROR_ID:
104                 task.log( LogChute.ERROR_PREFIX + message, Project.MSG_ERR );
105                 break;
106             default:
107                 task.log( message );
108                 break;
109         }
110     }
111 
112     /**
113      * <p>
114      * Log throwables through the Ant Task log method.  The mapping of logging
115      * levels from Velocity to Ant is as follows:
116      * </p>
117      *
118      * <blockquote><pre>
119      * Velocity Level      --&gt;  Ant Level
120      * LogSystem.TRACE_ID  --&gt;  Project.MSG_DEBUG
121      * LogSystem.DEBUG_ID  --&gt;  Project.MSG_DEBUG
122      * LogSystem.INFO_ID   --&gt;  Project.MSG_VERBOSE
123      * LogSystem.WARN_ID   --&gt;  Project.MSG_WARN
124      * LogSystem.ERROR_ID  --&gt;  Project.MSG_ERR
125      * </pre></blockquote>
126      *
127      * @param level    severity level
128      * @param message  complete error message
129      * @param throwable the throwable object to log
130      * @see   org.apache.velocity.runtime.log.LogChute
131      * @see   org.apache.tools.ant.Task#log(String, int)
132      */
133     public void log(int level, String message, Throwable throwable) {
134         switch ( level )
135         {
136             case LogChute.TRACE_ID:
137                 task.log( LogChute.TRACE_PREFIX + message, throwable, Project.MSG_DEBUG);
138                 break;
139             case LogChute.DEBUG_ID:
140                 task.log( LogChute.DEBUG_PREFIX + message, throwable, Project.MSG_DEBUG );
141                 break;
142             case LogChute.INFO_ID:
143                 task.log( LogChute.INFO_PREFIX + message, throwable, Project.MSG_VERBOSE );
144                 break;
145             case LogChute.WARN_ID:
146                 task.log( LogChute.WARN_PREFIX + message, throwable, Project.MSG_WARN );
147                 break;
148             case LogChute.ERROR_ID:
149                 task.log( LogChute.ERROR_PREFIX + message, throwable, Project.MSG_ERR );
150                 break;
151             default:
152                 task.log( message );
153                 break;
154         }
155     }
156 
157     public boolean isLevelEnabled(int level) {
158         return true;
159     }
160 
161     public void logVelocityMessage( int level, String message )
162     {
163     }
164 }