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