View Javadoc

1   package org.apache.velocity.tools.config;
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.runtime.log.Log;
23  
24  /**
25   * This allows for a {@link Log} to optionally be attached to
26   * a subclass in order to output logging messages. This is simpler
27   * than constantly checking whether we have a Log or not in each
28   * usage throughout the classes which could use logging.  Methods should
29   * only be added to this as necessary.  Performance considerations
30   * can also be made later if deemed necessary.  This is meant for internal
31   * use and should NOT be relied upon by VelocityTools users.
32   *
33   * @author Nathan Bubna
34   * @version $Id: LogSupport.java 511959 2007-02-26 19:24:39Z nbubna $
35   */
36  public abstract class LogSupport
37  {
38      private static final String DEFAULT_PREFIX = "";
39      private Log log;
40  
41      /**
42       * Override this to set a class-specific prefix
43       */
44      protected String logPrefix()
45      {
46          return DEFAULT_PREFIX;
47      }
48  
49      public void setLog(Log log)
50      {
51          this.log = log;
52      }
53  
54      protected Log getLog()
55      {
56          return this.log;
57      }
58  
59      protected boolean isWarnEnabled()
60      {
61          return (log != null && log.isWarnEnabled());
62      }
63  
64      protected void warn(String msg)
65      {
66          if (isWarnEnabled())
67          {
68              log.warn(logPrefix() + msg);
69          }
70      }
71  
72      protected boolean isDebugEnabled()
73      {
74          return (log != null && log.isDebugEnabled());
75      }
76  
77      protected void debug(String msg)
78      {
79          if (isDebugEnabled())
80          {
81              log.debug(logPrefix() + msg);
82          }
83      }
84  
85      protected boolean isTraceEnabled()
86      {
87          return (log != null && log.isTraceEnabled());
88      }
89  
90      protected void trace(String msg)
91      {
92          if (isTraceEnabled())
93          {
94              log.trace(logPrefix() + msg);
95          }
96      }
97  
98  }