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   * This is a wrapper around a log object, that can add a prefix to log messages
24   * and also turn logging on and off dynamically. It is mainly used to control the
25   * logging of VelociMacro generation messages but is actually generic enough code.
26   *
27  
28   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
29   * @version $Id: LogDisplayWrapper.java 685685 2008-08-13 21:43:27Z nbubna $
30   * @since 1.5
31   */
32  public class LogDisplayWrapper
33          extends Log
34  {
35      /** The prefix to record with every log message */
36      private final String prefix;
37  
38      /** log messages only if true */
39      private final boolean outputMessages;
40  
41      /** The Log object we wrap */
42      private final Log log;
43  
44      /**
45       * Create a new LogDisplayWrapper
46       * @param log The Log object to wrap.
47       * @param prefix The prefix to record with all messages.
48       * @param outputMessages True when messages should actually get logged.
49       */
50      public LogDisplayWrapper(final Log log, final String prefix, final boolean outputMessages)
51      {
52          super(log.getLogChute());
53          this.log = log;
54          this.prefix = prefix;
55          this.outputMessages = outputMessages;
56      }
57  
58      /**
59       * make sure that we always use the right LogChute Object
60       */
61      protected LogChute getLogChute()
62      {
63          return log.getLogChute();
64      }
65  
66      /**
67       * @see Log#log(int, Object)
68       */
69      protected void log(final int level, final Object message)
70      {
71      	log(outputMessages, level, message);
72      }
73      
74      protected void log(final boolean doLogging, final int level, final Object message)
75      {
76          if (doLogging)
77          {
78              getLogChute().log(level, prefix + String.valueOf(message));
79          }
80      }
81  
82      /**
83       * @see Log#log(int, Object, Throwable)
84       */
85      protected void log(final int level, final Object message, final Throwable t)
86      {
87      	log(outputMessages, level, message);
88      }
89      
90      protected void log(final boolean doLogging, final int level, final Object message, final Throwable t)
91      {
92          if (doLogging)
93          {
94              getLogChute().log(level, prefix + String.valueOf(message), t);
95          }
96      }
97      
98      /**
99       * Log a trace message.
100      * @param doLogging Log only if this parameter is true.
101      * @param message
102      */
103     public void trace(final boolean doLogging, final Object message)
104     {
105         log(doLogging, LogChute.TRACE_ID, message);
106     }
107 
108     /**
109      * Log a trace message and accompanying Throwable.
110      * @param doLogging Log only if this parameter is true.
111      * @param message
112      * @param t
113      */
114     public void trace(final boolean doLogging, final Object message, final Throwable t)
115     {
116         log(doLogging, LogChute.TRACE_ID, message, t);
117     }
118 
119     /**
120      * Log a debug message.
121      * @param doLogging Log only if this parameter is true.
122      * @param message
123      */
124     public void debug(final boolean doLogging, final Object message)
125     {
126         log(doLogging, LogChute.DEBUG_ID, message);
127     }
128 
129     /**
130      * Log a debug message and accompanying Throwable.
131      * @param doLogging Log only if this parameter is true.
132      * @param message
133      * @param t
134      */
135     public void debug(final boolean doLogging, final Object message, final Throwable t)
136     {
137         log(doLogging, LogChute.DEBUG_ID, message, t);
138     }
139 
140     /**
141      * Log an info message.
142      * @param doLogging Log only if this parameter is true.
143      * @param message
144      */
145     public void info(final boolean doLogging, final Object message)
146     {
147         log(doLogging, LogChute.INFO_ID, message);
148     }
149 
150     /**
151      * Log an info message and accompanying Throwable.
152      * @param doLogging Log only if this parameter is true.
153      * @param message
154      * @param t
155      */
156     public void info(final boolean doLogging, final Object message, final Throwable t)
157     {
158         log(doLogging, LogChute.INFO_ID, message, t);
159     }
160 
161     /**
162      * Log a warning message.
163      * @param doLogging Log only if this parameter is true.
164      * @param message
165      */
166     public void warn(final boolean doLogging, final Object message)
167     {
168         log(doLogging, LogChute.WARN_ID, message);
169     }
170 
171     /**
172      * Log a warning message and accompanying Throwable.
173      * @param doLogging Log only if this parameter is true.
174      * @param message
175      * @param t
176      */
177     public void warn(final boolean doLogging, final Object message, final Throwable t)
178     {
179         log(doLogging, LogChute.WARN_ID, message, t);
180     }
181 
182     /**
183      * Log an error message.
184      * @param doLogging Log only if this parameter is true.
185      * @param message
186      */
187     public void error(final boolean doLogging, final Object message)
188     {
189         log(doLogging, LogChute.ERROR_ID, message);
190     }
191 
192     /**
193      * Log an error message and accompanying Throwable.
194      * @param doLogging Log only if this parameter is true.
195      * @param message
196      * @param t
197      */
198     public void error(final boolean doLogging, final Object message, final Throwable t)
199     {
200         log(doLogging, LogChute.ERROR_ID, message, t);
201     }
202 }
203