001: package org.apache.velocity.runtime.log;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.runtime.RuntimeServices;
023:
024: /**
025: * Logger used when no other is configured. By default, all messages
026: * will be printed to the System.err output stream.
027: *
028: * @author <a href="mailto:nbubna@apache.org">Nathan Bubna</a>
029: * @version $Id: SystemLogChute.java 463298 2006-10-12 16:10:32Z henning $
030: */
031: public class SystemLogChute implements LogChute {
032: public static final String RUNTIME_LOG_LEVEL_KEY = "runtime.log.logsystem.system.level";
033: public static final String RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY = "runtime.log.logsystem.system.err.level";
034:
035: private int enabled = TRACE_ID;
036: private int errLevel = TRACE_ID;
037:
038: public void init(RuntimeServices rs) throws Exception {
039: // look for a level config property
040: String level = (String) rs.getProperty(RUNTIME_LOG_LEVEL_KEY);
041: if (level != null) {
042: // and set it accordingly
043: setEnabledLevel(toLevel(level));
044: }
045:
046: // look for an errLevel config property
047: String errLevel = (String) rs
048: .getProperty(RUNTIME_LOG_SYSTEM_ERR_LEVEL_KEY);
049: if (errLevel != null) {
050: setSystemErrLevel(toLevel(errLevel));
051: }
052: }
053:
054: protected int toLevel(String level) {
055: if (level.equalsIgnoreCase("debug")) {
056: return DEBUG_ID;
057: } else if (level.equalsIgnoreCase("info")) {
058: return INFO_ID;
059: } else if (level.equalsIgnoreCase("warn")) {
060: return WARN_ID;
061: } else if (level.equalsIgnoreCase("error")) {
062: return ERROR_ID;
063: } else {
064: return TRACE_ID;
065: }
066: }
067:
068: protected String getPrefix(int level) {
069: switch (level) {
070: case WARN_ID:
071: return WARN_PREFIX;
072: case INFO_ID:
073: return INFO_PREFIX;
074: case DEBUG_ID:
075: return DEBUG_PREFIX;
076: case TRACE_ID:
077: return TRACE_PREFIX;
078: case ERROR_ID:
079: return ERROR_PREFIX;
080: default:
081: return INFO_PREFIX;
082: }
083: }
084:
085: /**
086: * Logs messages to either std.out or std.err
087: * depending on their severity.
088: *
089: * @param level severity level
090: * @param message complete error message
091: */
092: public void log(int level, String message) {
093: // pass it off
094: log(level, message, null);
095: }
096:
097: /**
098: * Logs messages to the system console so long as the specified level
099: * is equal to or greater than the level this LogChute is enabled for.
100: * If the level is equal to or greater than LogChute.ERROR_ID,
101: * messages will be printed to System.err. Otherwise, they will be
102: * printed to System.out. If a java.lang.Throwable accompanies the
103: * message, it's stack trace will be printed to the same stream
104: * as the message.
105: *
106: * @param level severity level
107: * @param message complete error message
108: * @param t the java.lang.Throwable
109: */
110: public void log(int level, String message, Throwable t) {
111: if (!isLevelEnabled(level)) {
112: return;
113: }
114:
115: String prefix = getPrefix(level);
116: if (level >= this .errLevel) {
117: System.err.print(prefix);
118: System.err.println(message);
119: if (t != null) {
120: System.err.println(t.getMessage());
121: t.printStackTrace();
122: }
123: } else {
124: System.out.print(prefix);
125: System.out.println(message);
126: if (t != null) {
127: System.out.println(t.getMessage());
128: t.printStackTrace(System.out);
129: }
130: }
131: }
132:
133: /**
134: * Set the minimum level at which messages will be printed.
135: */
136: public void setEnabledLevel(int level) {
137: this .enabled = level;
138: }
139:
140: /**
141: * Returns the current minimum level at which messages will be printed.
142: */
143: public int getEnabledLevel() {
144: return this .enabled;
145: }
146:
147: /**
148: * Set the minimum level at which messages will be printed to System.err
149: * instead of System.out.
150: */
151: public void setSystemErrLevel(int level) {
152: this .errLevel = level;
153: }
154:
155: /**
156: * Returns the current minimum level at which messages will be printed
157: * to System.err instead of System.out.
158: */
159: public int getSystemErrLevel() {
160: return this .errLevel;
161: }
162:
163: /**
164: * This will return true if the specified level
165: * is equal to or higher than the level this
166: * LogChute is enabled for.
167: */
168: public boolean isLevelEnabled(int level) {
169: return (level >= this.enabled);
170: }
171:
172: }
|