001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: * Copyright 2004 Costin Manolache
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.juli.logging;
019:
020: import java.util.logging.ConsoleHandler;
021: import java.util.logging.Formatter;
022: import java.util.logging.Handler;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: /**
027: * Hardcoded java.util.logging commons-logging implementation.
028: *
029: * In addition, it curr
030: *
031: */
032: class DirectJDKLog implements Log {
033: // no reason to hide this - but good reasons to not hide
034: public Logger logger;
035:
036: /** Alternate config reader and console format
037: */
038: private static final String SIMPLE_FMT = "org.apache.tomcat.util.log.JdkLoggerFormatter";
039: private static final String SIMPLE_CFG = "org.apache.tomcat.util.log.JdkLoggerConfig";
040:
041: static {
042: if (System.getProperty("java.util.logging.config.class") == null
043: && System.getProperty("java.util.logging.config.file") == null) {
044: // default configuration - it sucks. Let's override at least the
045: // formatter for the console
046: try {
047: Class.forName(SIMPLE_CFG).newInstance();
048: } catch (Throwable t) {
049: }
050: try {
051: Formatter fmt = (Formatter) Class.forName(SIMPLE_FMT)
052: .newInstance();
053: // it is also possible that the user modifed jre/lib/logging.properties -
054: // but that's really stupid in most cases
055: Logger root = Logger.getLogger("");
056: Handler handlers[] = root.getHandlers();
057: for (int i = 0; i < handlers.length; i++) {
058: // I only care about console - that's what's used in default config anyway
059: if (handlers[i] instanceof ConsoleHandler) {
060: handlers[i].setFormatter(fmt);
061: }
062: }
063: } catch (Throwable t) {
064: // maybe it wasn't included - the ugly default will be used.
065: }
066:
067: }
068: }
069:
070: public DirectJDKLog(String name) {
071: logger = Logger.getLogger(name);
072: }
073:
074: public final boolean isErrorEnabled() {
075: return logger.isLoggable(Level.SEVERE);
076: }
077:
078: public final boolean isWarnEnabled() {
079: return logger.isLoggable(Level.WARNING);
080: }
081:
082: public final boolean isInfoEnabled() {
083: return logger.isLoggable(Level.INFO);
084: }
085:
086: public final boolean isDebugEnabled() {
087: return logger.isLoggable(Level.FINE);
088: }
089:
090: public final boolean isFatalEnabled() {
091: return logger.isLoggable(Level.SEVERE);
092: }
093:
094: public final boolean isTraceEnabled() {
095: return logger.isLoggable(Level.FINER);
096: }
097:
098: public final void debug(Object message) {
099: log(Level.FINE, String.valueOf(message), null);
100: }
101:
102: public final void debug(Object message, Throwable t) {
103: log(Level.FINE, String.valueOf(message), t);
104: }
105:
106: public final void trace(Object message) {
107: log(Level.FINER, String.valueOf(message), null);
108: }
109:
110: public final void trace(Object message, Throwable t) {
111: log(Level.FINER, String.valueOf(message), t);
112: }
113:
114: public final void info(Object message) {
115: log(Level.INFO, String.valueOf(message), null);
116: }
117:
118: public final void info(Object message, Throwable t) {
119: log(Level.INFO, String.valueOf(message), t);
120: }
121:
122: public final void warn(Object message) {
123: log(Level.WARNING, String.valueOf(message), null);
124: }
125:
126: public final void warn(Object message, Throwable t) {
127: log(Level.WARNING, String.valueOf(message), t);
128: }
129:
130: public final void error(Object message) {
131: log(Level.SEVERE, String.valueOf(message), null);
132: }
133:
134: public final void error(Object message, Throwable t) {
135: log(Level.SEVERE, String.valueOf(message), t);
136: }
137:
138: public final void fatal(Object message) {
139: log(Level.SEVERE, String.valueOf(message), null);
140: }
141:
142: public final void fatal(Object message, Throwable t) {
143: log(Level.SEVERE, String.valueOf(message), t);
144: }
145:
146: // from commons logging. This would be my number one reason why java.util.logging
147: // is bad - design by comitee can be really bad ! The impact on performance of
148: // using java.util.logging - and the ugliness if you need to wrap it - is far
149: // worse than the unfriendly and uncommon default format for logs.
150:
151: private void log(Level level, String msg, Throwable ex) {
152: if (logger.isLoggable(level)) {
153: // Hack (?) to get the stack trace.
154: Throwable dummyException = new Throwable();
155: StackTraceElement locations[] = dummyException
156: .getStackTrace();
157: // Caller will be the third element
158: String cname = "unknown";
159: String method = "unknown";
160: if (locations != null && locations.length > 2) {
161: StackTraceElement caller = locations[2];
162: cname = caller.getClassName();
163: method = caller.getMethodName();
164: }
165: if (ex == null) {
166: logger.logp(level, cname, method, msg);
167: } else {
168: logger.logp(level, cname, method, msg, ex);
169: }
170: }
171: }
172:
173: // for LogFactory
174: static void release() {
175:
176: }
177:
178: static Log getInstance(String name) {
179: return new DirectJDKLog(name);
180: }
181: }
|