001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.velocity.tools.test;
018:
019: import java.io.PrintWriter;
020: import java.io.IOException;
021:
022: import org.mortbay.log.Logger;
023: import org.mortbay.util.DateCache;
024:
025: /** Basic Jetty logger for our showcase webapp
026: *
027: * @author <a href=mailto:cbrisson@apache.org>Claude Brisson</a>
028: */
029:
030: public class JettyLogger implements Logger {
031:
032: private boolean debug = false;
033: private String name = null;
034: private DateCache _dateCache = new DateCache(
035: "yyyy-MM-dd HH:mm:ss.SSS");
036:
037: private static PrintWriter out = null;
038:
039: static {
040: try {
041: String logfile = System.getProperty("jetty.log.file",
042: "/tmp/error.log");
043: out = new PrintWriter(logfile);
044: } catch (IOException ioe) {
045: System.out.println(ioe.getMessage());
046: }
047: }
048:
049: public JettyLogger() {
050: this (null);
051: }
052:
053: public JettyLogger(String name) {
054: this .name = name == null ? "" : name;
055: }
056:
057: /*
058: * org.mortbay.log.Logger interface
059: */
060:
061: public boolean isDebugEnabled() {
062: return debug;
063: }
064:
065: public void setDebugEnabled(boolean enabled) {
066: debug = enabled;
067: }
068:
069: public void info(String msg, Object arg0, Object arg1) {
070: if (out == null)
071: return;
072: /* a bit of filtering in debug mode */
073: if (debug
074: && (msg.startsWith("loaded class") || msg
075: .startsWith("loaded interface"))) {
076: return;
077: }
078: logString(_dateCache.now() + " " + name + " "
079: + format(msg, arg0, arg1));
080: }
081:
082: public void debug(String msg, Throwable th) {
083: if (debug) {
084: if (out == null)
085: return;
086: /* a bit of filtering in debug mode */
087: if (debug
088: && (msg.startsWith("loaded class") || msg
089: .startsWith("loaded interface"))) {
090: return;
091: }
092: logString(_dateCache.now() + " " + msg);
093: logStackTrace(th);
094: }
095: }
096:
097: public void debug(String msg, Object arg0, Object arg1) {
098: if (debug) {
099: if (out == null)
100: return;
101: /* a bit of filtering in debug mode */
102: if (debug
103: && (msg.startsWith("loaded class") || msg
104: .startsWith("loaded interface"))) {
105: return;
106: }
107: logString(_dateCache.now() + " " + format(msg, arg0, arg1));
108: }
109: }
110:
111: public void warn(String msg, Object arg0, Object arg1) {
112: if (out == null)
113: return;
114: logString(_dateCache.now() + " " + format(msg, arg0, arg1));
115: }
116:
117: public void warn(String msg, Throwable th) {
118: if (out == null)
119: return;
120: logString(_dateCache.now() + " " + msg);
121: logStackTrace(th);
122: }
123:
124: public Logger getLogger(String name) {
125: if ((name == null && this .name == null)
126: || (name != null && name.equals(this .name)))
127: return this ;
128: return new JettyLogger(name);
129: }
130:
131: /*
132: * private helpers
133: */
134:
135: private synchronized void logString(String msg) {
136: out.println(msg);
137: out.flush();
138: }
139:
140: private synchronized void logStackTrace(Throwable th) {
141: th.printStackTrace(out);
142: out.flush();
143: }
144:
145: private String format(String msg, Object arg0, Object arg1) {
146: int i0 = msg.indexOf("{}");
147: int i1 = i0 < 0 ? -1 : msg.indexOf("{}", i0 + 2);
148:
149: if (arg1 != null && i1 >= 0)
150: msg = msg.substring(0, i1) + arg1 + msg.substring(i1 + 2);
151: if (arg0 != null && i0 >= 0)
152: msg = msg.substring(0, i0) + arg0 + msg.substring(i0 + 2);
153: return msg;
154: }
155:
156: }
|