001: package org.apache.velocity.test.misc;
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: import org.apache.velocity.runtime.log.LogChute;
024:
025: /**
026: * LogChute implementation that creates a String in memory. Used to test
027: * log information.
028: *
029: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
030: * @version $Id: TestLogChute.java 490718 2006-12-28 13:35:49Z wglass $
031: */
032: public class TestLogChute implements LogChute {
033: public static final String TEST_LOGGER_LEVEL = "runtime.log.logsystem.test.level";
034:
035: private StringBuffer log = new StringBuffer();
036:
037: private int logLevel;
038:
039: public void init(RuntimeServices rs) throws Exception {
040: String level = rs.getString(TEST_LOGGER_LEVEL, "debug");
041: logLevel = getLevelNumber(level, LogChute.DEBUG_ID);
042: }
043:
044: public void log(int level, String message) {
045: if (level >= logLevel) {
046: String levelName;
047: levelName = getLevelName(level);
048: log.append(" [").append(levelName).append("] ");
049: log.append(message);
050: log.append("\n");
051: }
052: }
053:
054: /**
055: * Return the stored log messages to date.
056: * @return log messages
057: */
058: public String getLog() {
059: return log.toString();
060: }
061:
062: /**
063: * Return the name corresponding to each level
064: * @param level integer level
065: * @return String level name
066: */
067: private String getLevelName(int level) {
068: String levelName;
069: if (level == LogChute.DEBUG_ID) {
070: levelName = "debug";
071: } else if (level == LogChute.INFO_ID) {
072: levelName = "info";
073: } else if (level == LogChute.TRACE_ID) {
074: levelName = "trace";
075: } else if (level == LogChute.WARN_ID) {
076: levelName = "warn";
077: } else if (level == LogChute.ERROR_ID) {
078: levelName = "error";
079: } else {
080: levelName = "";
081: }
082:
083: return levelName;
084: }
085:
086: /**
087: * Return the integer level correspoding to the string number, or use the default
088: * @param level name
089: * @param defaultLevel the default if the name does not exist
090: * @return integer level
091: */
092: private int getLevelNumber(String level, int defaultLevel) {
093: if (level == null) {
094: return defaultLevel;
095: } else if (level.equalsIgnoreCase("DEBUG")) {
096: return LogChute.DEBUG_ID;
097: } else if (level.equalsIgnoreCase("ERROR")) {
098: return LogChute.ERROR_ID;
099: } else if (level.equalsIgnoreCase("INFO")) {
100: return LogChute.INFO_ID;
101: } else if (level.equalsIgnoreCase("TRACE")) {
102: return LogChute.TRACE_ID;
103: } else if (level.equalsIgnoreCase("WARN")) {
104: return LogChute.WARN_ID;
105: } else {
106: return defaultLevel;
107: }
108: }
109:
110: public void log(int level, String message, Throwable t) {
111: if (level >= logLevel) {
112: String levelName;
113: levelName = getLevelName(level);
114: log.append(" [").append(levelName).append("] ");
115: log.append(message);
116: log.append("\n");
117: log.append(t.toString());
118: log.append("\n");
119: }
120: }
121:
122: public boolean isLevelEnabled(int level) {
123: return level >= logLevel;
124: }
125:
126: }
|