001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
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: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.log;
016:
017: import org.mortbay.util.DateCache;
018:
019: /*-----------------------------------------------------------------------*/
020: /** StdErr Logging.
021: * This implementation of the Logging facade sends all logs to StdErr with minimal formatting.
022: *
023: * If the system property DEBUG is set, then debug logs are printed if stderr is being used.
024: *
025: */
026: public class StdErrLog implements Logger {
027: private static DateCache _dateCache;
028: private static boolean debug = System.getProperty("DEBUG", null) != null;
029: private String name;
030:
031: static {
032: try {
033: _dateCache = new DateCache("yyyy-MM-dd HH:mm:ss.SSS");
034: } catch (Exception e) {
035: e.printStackTrace();
036: }
037:
038: }
039:
040: StdErrLog() {
041: this (null);
042: }
043:
044: StdErrLog(String name) {
045: this .name = name == null ? "" : name;
046: }
047:
048: public boolean isDebugEnabled() {
049: return debug;
050: }
051:
052: public void setDebugEnabled(boolean enabled) {
053: debug = enabled;
054: }
055:
056: public void info(String msg, Object arg0, Object arg1) {
057: System.err.println(_dateCache.now() + ":" + name + ":INFO: "
058: + format(msg, arg0, arg1));
059: }
060:
061: public void debug(String msg, Throwable th) {
062: if (debug) {
063: System.err.println(_dateCache.now() + ":" + name
064: + ":DEBUG: " + msg);
065: if (th != null)
066: th.printStackTrace();
067: }
068: }
069:
070: public void debug(String msg, Object arg0, Object arg1) {
071: if (debug) {
072: System.err.println(_dateCache.now() + ":" + name
073: + ":DEBUG: " + format(msg, arg0, arg1));
074: }
075: }
076:
077: public void warn(String msg, Object arg0, Object arg1) {
078: System.err.println(_dateCache.now() + ":" + name + ":WARN: "
079: + format(msg, arg0, arg1));
080: }
081:
082: public void warn(String msg, Throwable th) {
083: System.err.println(_dateCache.now() + ":" + name + ":WARN: "
084: + msg);
085: if (th != null)
086: th.printStackTrace();
087: }
088:
089: private String format(String msg, Object arg0, Object arg1) {
090: int i0 = msg.indexOf("{}");
091: int i1 = i0 < 0 ? -1 : msg.indexOf("{}", i0 + 2);
092:
093: if (arg1 != null && i1 >= 0)
094: msg = msg.substring(0, i1) + arg1 + msg.substring(i1 + 2);
095: if (arg0 != null && i0 >= 0)
096: msg = msg.substring(0, i0) + arg0 + msg.substring(i0 + 2);
097: return msg;
098: }
099:
100: public Logger getLogger(String name) {
101: if ((name == null && this .name == null)
102: || (name != null && name.equals(this .name)))
103: return this ;
104: return new StdErrLog(name);
105: }
106:
107: public String toString() {
108: return "STDERR" + name;
109: }
110:
111: }
|