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 java.io.PrintStream;
23  import org.apache.velocity.runtime.RuntimeServices;
24  
25  /**
26   * Logger used when no other is configured.  By default, all messages
27   * will be printed to the System.err output stream.
28   *
29   * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
30   * @version $Id: SystemLogChute.java 730039 2008-12-30 03:53:19Z byron $
31   * @since 1.5
32   */
33  public class SystemLogChute implements LogChute
34  {
35      public static final String RUNTIME_LOG_LEVEL_KEY = 
36          "runtime.log.logsystem.system.level";
37      public static final String RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY = 
38          "runtime.log.logsystem.system.err.level";
39  
40      private int enabled = WARN_ID;
41      private int errLevel = TRACE_ID;
42  
43      public void init(RuntimeServices rs) throws Exception
44      {
45          // look for a level config property
46          String level = (String)rs.getProperty(RUNTIME_LOG_LEVEL_KEY);
47          if (level != null)
48          {
49              // and set it accordingly
50              setEnabledLevel(toLevel(level));
51          }
52  
53          // look for an errLevel config property
54          String errLevel = (String)rs.getProperty(RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY);
55          if (errLevel != null)
56          {
57              setSystemErrLevel(toLevel(errLevel));
58          }
59      }
60  
61      protected int toLevel(String level) {
62          if (level.equalsIgnoreCase("debug"))
63          {
64              return DEBUG_ID;
65          }
66          else if (level.equalsIgnoreCase("info"))
67          {
68              return INFO_ID;
69          }
70          else if (level.equalsIgnoreCase("warn"))
71          {
72              return WARN_ID;
73          }
74          else if (level.equalsIgnoreCase("error"))
75          {
76              return ERROR_ID;
77          }
78          else
79          {
80              return TRACE_ID;
81          }
82      }
83  
84      protected String getPrefix(int level)
85      {
86          switch (level)
87          {
88              case WARN_ID:
89                  return WARN_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              case INFO_ID:
97              default:
98                  return INFO_PREFIX;
99          }
100     }
101 
102     /**
103      * Logs messages to either std.out or std.err
104      * depending on their severity.
105      *
106      * @param level severity level
107      * @param message complete error message
108      */
109     public void log(int level, String message)
110     {
111         // pass it off
112         log(level, message, null);
113     }
114 
115     /**
116      * Logs messages to the system console so long as the specified level
117      * is equal to or greater than the level this LogChute is enabled for.
118      * If the level is equal to or greater than LogChute.ERROR_ID, 
119      * messages will be printed to System.err. Otherwise, they will be 
120      * printed to System.out. If a java.lang.Throwable accompanies the 
121      * message, it's stack trace will be printed to the same stream
122      * as the message.
123      *
124      * @param level severity level
125      * @param message complete error message
126      * @param t the java.lang.Throwable
127      */
128     public void log(int level, String message, Throwable t)
129     {
130         if (!isLevelEnabled(level))
131         {
132             return;
133         }
134 
135         String prefix = getPrefix(level);
136         if (level >= this.errLevel)
137         {
138             write(System.err, prefix, message, t);
139         }
140         else
141         {
142             write(System.out, prefix, message, t);
143         }
144     }
145 
146     protected void write(PrintStream stream, String prefix, String message, Throwable t)
147     {
148         stream.print(prefix);
149         stream.println(message);
150         if (t != null)
151         {
152             stream.println(t.getMessage());
153             t.printStackTrace(stream);
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 }