View Javadoc

1   package org.apache.velocity.tools.generic.log;
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.velocity.app.Velocity;
23  import org.apache.velocity.runtime.log.Log;
24  
25  /**
26   * Redirects commons-logging messages to Velocity's configured LogChute.
27   *
28   * <p>To use, specify this class in your commons-logging.properties:
29   * <pre><code>
30   * org.apache.commons.logging.Log=org.apache.velocity.tools.generic.log.LogChuteCommonsLog
31   * </code></pre>
32   * </p>
33   * @since VelocityTools 2.0
34   * @version $Id: LogChuteCommonsLog.java 72115 2004-11-11 07:00:54Z nbubna $
35   */
36  public class LogChuteCommonsLog implements org.apache.commons.logging.Log
37  {
38  
39      private static Log target = null;
40  
41      /**
42       * Allow subclasses to statically access the static target.
43       */
44      protected static Log getVelocityLog()
45      {
46          return target;
47      }
48  
49      /**
50       * Set a VelocityEngine to handle all the log messages.
51       */
52      public static void setVelocityLog(Log target)
53      {
54          LogChuteCommonsLog.target = target;
55      }
56  
57  
58      // ********************  begin non-static stuff *******************
59  
60      private String category;
61  
62      public LogChuteCommonsLog()
63      {
64          this("");
65      }
66  
67      public LogChuteCommonsLog(String category)
68      {
69          this.category = category + ": ";
70      }
71  
72      protected Log getTarget()
73      {
74          if (target == null)
75          {
76              return Velocity.getLog();
77          }
78          else
79          {
80              return target;
81          }
82      }
83  
84  
85      /*************** Commons Log Interface ****************/
86  
87      /**
88       * Passes messages to Velocity's LogChute at "DEBUG" level.
89       * (it's the lowest available. sorry.)
90       */
91      public void trace(Object message)
92      {
93          getTarget().trace(category+message);
94      }
95  
96      /**
97       * Passes messages to Velocity's LogChute at "DEBUG" level.
98       * (it's the lowest available. sorry.)
99       */
100     public void trace(Object message, Throwable t)
101     {
102         getTarget().trace(category+message, t);
103     }
104 
105     /**
106      * Passes messages to Velocity's LogChute at "DEBUG" level.
107      */
108     public void debug(Object message)
109     {
110         getTarget().debug(category+message);
111     }
112 
113     /**
114      * Passes messages to Velocity's LogChute at "DEBUG" level.
115      */
116     public void debug(Object message, Throwable t)
117     {
118         getTarget().debug(category+message, t);
119     }
120 
121     /**
122      * Passes messages to Velocity's LogChute at "INFO" level.
123      */
124     public void info(Object message)
125     {
126         getTarget().info(category+message);
127     }
128 
129     /**
130      * Passes messages to Velocity's LogChute at "INFO" level.
131      */
132     public void info(Object message, Throwable t)
133     {
134         getTarget().info(category+message, t);
135     }
136 
137     /**
138      * Passes messages to Velocity's LogChute at "WARN" level.
139      */
140     public void warn(Object message)
141     {
142         getTarget().warn(category+message);
143     }
144 
145     /**
146      * Passes messages to Velocity's LogChute at "WARN" level.
147      */
148     public void warn(Object message, Throwable t)
149     {
150         getTarget().warn(category+message, t);
151     }
152 
153     /**
154      * Passes messages to Velocity's LogChute at "ERROR" level.
155      */
156     public void error(Object message)
157     {
158         getTarget().error(category+message);
159     }
160 
161     /**
162      * Passes messages to Velocity's LogChute at "ERROR" level.
163      */
164     public void error(Object message, Throwable t)
165     {
166         getTarget().error(category+message, t);
167     }
168 
169     /**
170      * Passes messages to Velocity's LogChute at "ERROR" level.
171      * (it's the highest available. sorry.)
172      */
173     public void fatal(Object message)
174     {
175         getTarget().error(category+message);
176     }
177 
178     /**
179      * Passes messages to Velocity's LogChute at "ERROR" level.
180      * (it's the highest available. sorry.)
181      */
182     public void fatal(Object message, Throwable t)
183     {
184         getTarget().error(category+message, t);
185     }
186 
187     /** 
188      * Returns true if Velocity's LogChute returns true 
189      * for isTraceEnabled().
190      */
191     public boolean isTraceEnabled()
192     {
193         return getTarget().isTraceEnabled();
194     }
195 
196     /** 
197      * Returns true if Velocity's LogChute returns true 
198      * for isDebugEnabled().
199      */
200     public boolean isDebugEnabled()
201     {
202         return getTarget().isDebugEnabled();
203     }
204 
205     /** 
206      * Returns true if Velocity's LogChute returns true 
207      * for isInfoEnabled().
208      */
209     public boolean isInfoEnabled()
210     {
211         return getTarget().isInfoEnabled();
212     }
213 
214     /** 
215      * Returns true if Velocity's LogChute returns true 
216      * for isWarnEnabled().
217      */
218     public boolean isWarnEnabled()
219     {
220         return getTarget().isWarnEnabled();
221     }
222 
223     /** 
224      * Returns true if Velocity's LogChute returns true 
225      * for isErrorEnabled().
226      */
227     public boolean isErrorEnabled()
228     {
229         return getTarget().isErrorEnabled();
230     }
231 
232     /** 
233      * Returns true if isErrorEnabled() returns true, since
234      * Velocity's LogChute doesn't support this level.
235      */
236     public boolean isFatalEnabled()
237     {
238         return isErrorEnabled();
239     }
240 
241 }