View Javadoc

1   package org.apache.velocity.runtime.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  /**
23   * Convenient wrapper for LogChute functions. This implements
24   * the RuntimeLogger methods (and then some).  It is hoped that
25   * use of this will fully replace use of the RuntimeLogger.
26   *
27   * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
28   * @version $Id: Log.java 469919 2006-11-01 14:35:46Z henning $
29   */
30  public class Log
31  {
32  
33      private LogChute chute;
34  
35      /**
36       * Creates a new Log that wraps a HoldingLogChute.
37       */
38      public Log()
39      {
40          setLogChute(new HoldingLogChute());
41      }
42  
43      /**
44       * Creates a new Log that wraps the specified LogChute.
45       * @param chute
46       */
47      public Log(final LogChute chute)
48      {
49          setLogChute(chute);
50      }
51  
52      /**
53       * Updates the LogChute wrapped by this Log instance.
54       * @param chute The new value for the log chute.
55       */
56      protected void setLogChute(final LogChute chute)
57      {
58          if (chute == null)
59          {
60              throw new NullPointerException("The LogChute cannot be set to null!");
61          }
62          this.chute = chute;
63      }
64  
65      /**
66       * Returns the LogChute wrapped by this Log instance.
67       * @return The LogChute wrapped by this Log instance.
68       */
69      protected LogChute getLogChute()
70      {
71          return this.chute;
72      }
73  
74      protected void log(int level, Object message)
75      {
76          getLogChute().log(level, String.valueOf(message));
77      }
78  
79      protected void log(int level, Object message, Throwable t)
80      {
81          getLogChute().log(level, String.valueOf(message), t);
82      }
83  
84      /**
85       * Returns true if trace level messages will be printed by the LogChute.
86       * @return If trace level messages will be printed by the LogChute.
87       */
88      public boolean isTraceEnabled()
89      {
90          return getLogChute().isLevelEnabled(LogChute.TRACE_ID);
91      }
92  
93      /**
94       * Log a trace message.
95       * @param message
96       */
97      public void trace(Object message)
98      {
99          log(LogChute.TRACE_ID, message);
100     }
101 
102     /**
103      * Log a trace message and accompanying Throwable.
104      * @param message
105      * @param t
106      */
107     public void trace(Object message, Throwable t)
108     {
109         log(LogChute.TRACE_ID, message, t);
110     }
111 
112     /**
113      * Returns true if debug level messages will be printed by the LogChute.
114      * @return True if debug level messages will be printed by the LogChute.
115      */
116     public boolean isDebugEnabled()
117     {
118         return getLogChute().isLevelEnabled(LogChute.DEBUG_ID);
119     }
120 
121     /**
122      * Log a debug message.
123      * @param message
124      */
125     public void debug(Object message)
126     {
127         log(LogChute.DEBUG_ID, message);
128     }
129 
130     /**
131      * Log a debug message and accompanying Throwable.
132      * @param message
133      * @param t
134      */
135     public void debug(Object message, Throwable t)
136     {
137         log(LogChute.DEBUG_ID, message, t);
138     }
139 
140     /**
141      * Returns true if info level messages will be printed by the LogChute.
142      * @return True if info level messages will be printed by the LogChute.
143      */
144     public boolean isInfoEnabled()
145     {
146         return getLogChute().isLevelEnabled(LogChute.INFO_ID);
147     }
148 
149     /**
150      * Log an info message.
151      * @param message
152      */
153     public void info(Object message)
154     {
155         log(LogChute.INFO_ID, message);
156     }
157 
158     /**
159      * Log an info message and accompanying Throwable.
160      * @param message
161      * @param t
162      */
163     public void info(Object message, Throwable t)
164     {
165         log(LogChute.INFO_ID, message, t);
166     }
167 
168     /**
169      * Returns true if warn level messages will be printed by the LogChute.
170      * @return True if warn level messages will be printed by the LogChute.
171      */
172     public boolean isWarnEnabled()
173     {
174         return getLogChute().isLevelEnabled(LogChute.WARN_ID);
175     }
176 
177     /**
178      * Log a warning message.
179      * @param message
180      */
181     public void warn(Object message)
182     {
183         log(LogChute.WARN_ID, message);
184     }
185 
186     /**
187      * Log a warning message and accompanying Throwable.
188      * @param message
189      * @param t
190      */
191     public void warn(Object message, Throwable t)
192     {
193         log(LogChute.WARN_ID, message, t);
194     }
195 
196     /**
197      * Returns true if error level messages will be printed by the LogChute.
198      * @return True if error level messages will be printed by the LogChute.
199      */
200     public boolean isErrorEnabled()
201     {
202         return getLogChute().isLevelEnabled(LogChute.ERROR_ID);
203     }
204 
205     /**
206      * Log an error message.
207      * @param message
208      */
209     public void error(Object message)
210     {
211         log(LogChute.ERROR_ID, message);
212     }
213 
214     /**
215      * Log an error message and accompanying Throwable.
216      * @param message
217      * @param t
218      */
219     public void error(Object message, Throwable t)
220     {
221         log(LogChute.ERROR_ID, message, t);
222     }
223 
224 }