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  import org.apache.velocity.runtime.RuntimeServices;
23  
24  /**
25   * Logger used when no other is configured.  By default, all messages
26   * will be printed to the System.err output stream.
27   *
28   * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
29   * @version $Id: SystemLogChute.java 522566 2007-03-26 16:42:00Z nbubna $
30   */
31  public class SystemLogChute implements LogChute
32  {
33      public static final String RUNTIME_LOG_LEVEL_KEY = 
34          "runtime.log.logsystem.system.level";
35      public static final String RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY = 
36          "runtime.log.logsystem.system.err.level";
37  
38      private int enabled = WARN_ID;
39      private int errLevel = TRACE_ID;
40  
41      public void init(RuntimeServices rs) throws Exception
42      {
43          // look for a level config property
44          String level = (String)rs.getProperty(RUNTIME_LOG_LEVEL_KEY);
45          if (level != null)
46          {
47              // and set it accordingly
48              setEnabledLevel(toLevel(level));
49          }
50  
51          // look for an errLevel config property
52          String errLevel = (String)rs.getProperty(RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY);
53          if (errLevel != null)
54          {
55              setSystemErrLevel(toLevel(errLevel));
56          }
57      }
58  
59      protected int toLevel(String level) {
60          if (level.equalsIgnoreCase("debug"))
61          {
62              return DEBUG_ID;
63          }
64          else if (level.equalsIgnoreCase("info"))
65          {
66              return INFO_ID;
67          }
68          else if (level.equalsIgnoreCase("warn"))
69          {
70              return WARN_ID;
71          }
72          else if (level.equalsIgnoreCase("error"))
73          {
74              return ERROR_ID;
75          }
76          else
77          {
78              return TRACE_ID;
79          }
80      }
81  
82      protected String getPrefix(int level)
83      {
84          switch (level)
85          {
86              case WARN_ID:
87                  return WARN_PREFIX;
88              case INFO_ID:
89                  return INFO_PREFIX ;
90              case DEBUG_ID:
91                  return DEBUG_PREFIX;
92              case TRACE_ID:
93                  return TRACE_PREFIX;
94              case ERROR_ID:
95                  return ERROR_PREFIX;
96              default:
97                  return INFO_PREFIX;
98          }
99      }
100 
101     /**
102      * Logs messages to either std.out or std.err
103      * depending on their severity.
104      *
105      * @param level severity level
106      * @param message complete error message
107      */
108     public void log(int level, String message)
109     {
110         // pass it off
111         log(level, message, null);
112     }
113 
114     /**
115      * Logs messages to the system console so long as the specified level
116      * is equal to or greater than the level this LogChute is enabled for.
117      * If the level is equal to or greater than LogChute.ERROR_ID, 
118      * messages will be printed to System.err. Otherwise, they will be 
119      * printed to System.out. If a java.lang.Throwable accompanies the 
120      * message, it's stack trace will be printed to the same stream
121      * as the message.
122      *
123      * @param level severity level
124      * @param message complete error message
125      * @param t the java.lang.Throwable
126      */
127     public void log(int level, String message, Throwable t)
128     {
129         if (!isLevelEnabled(level))
130         {
131             return;
132         }
133 
134         String prefix = getPrefix(level);
135         if (level >= this.errLevel)
136         {
137             System.err.print(prefix);
138             System.err.println(message);
139             if (t != null)
140             {
141                 System.err.println(t.getMessage());
142                 t.printStackTrace();
143             }
144         }
145         else
146         {
147             System.out.print(prefix);
148             System.out.println(message);
149             if (t != null)
150             {
151                 System.out.println(t.getMessage());
152                 t.printStackTrace(System.out);
153             }
154         }
155     }
156 
157     /**
158      * Set the minimum level at which messages will be printed.
159      */
160     public void setEnabledLevel(int level)
161     {
162         this.enabled = level;
163     }
164 
165     /**
166      * Returns the current minimum level at which messages will be printed.
167      */
168     public int getEnabledLevel()
169     {
170         return this.enabled;
171     }
172 
173     /**
174      * Set the minimum level at which messages will be printed to System.err
175      * instead of System.out.
176      */
177     public void setSystemErrLevel(int level)
178     {
179         this.errLevel = level;
180     }
181 
182     /**
183      * Returns the current minimum level at which messages will be printed
184      * to System.err instead of System.out.
185      */
186     public int getSystemErrLevel()
187     {
188         return this.errLevel;
189     }
190 
191     /**
192      * This will return true if the specified level
193      * is equal to or higher than the level this
194      * LogChute is enabled for.
195      */
196     public boolean isLevelEnabled(int level)
197     {
198         return (level >= this.enabled);
199     }
200 
201 }