001: /*
002: * Copyright 2001-2004 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.commons.logging.impl;
018:
019: import java.io.Serializable;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022: import java.util.logging.LogRecord;
023: import java.util.StringTokenizer;
024: import java.io.PrintWriter;
025: import java.io.StringWriter;
026:
027: import org.apache.commons.logging.Log;
028:
029: /**
030: * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
031: * interface that wraps the standard JDK logging mechanisms that are
032: * available in SourceForge's Lumberjack for JDKs prior to 1.4.</p>
033: *
034: * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
035: * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
036: * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
037: * @author <a href="mailto:vince256@comcast.net">Vince Eagen</a>
038: * @version $Revision: 371403 $ $Date: 2006-01-22 22:07:31 +0000 (Sun, 22 Jan 2006) $
039: * @since 1.1
040: */
041:
042: public class Jdk13LumberjackLogger implements Log, Serializable {
043:
044: // ----------------------------------------------------- Instance Variables
045:
046: /**
047: * The underlying Logger implementation we are using.
048: */
049: protected transient Logger logger = null;
050: protected String name = null;
051: private String sourceClassName = "unknown";
052: private String sourceMethodName = "unknown";
053: private boolean classAndMethodFound = false;
054:
055: /**
056: * This member variable simply ensures that any attempt to initialise
057: * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
058: * It must not be private, as an optimising compiler could detect that it
059: * is not used and optimise it away.
060: */
061: protected static final Level dummyLevel = Level.FINE;
062:
063: // ----------------------------------------------------------- Constructors
064:
065: /**
066: * Construct a named instance of this Logger.
067: *
068: * @param name Name of the logger to be constructed
069: */
070: public Jdk13LumberjackLogger(String name) {
071:
072: this .name = name;
073: logger = getLogger();
074:
075: }
076:
077: // --------------------------------------------------------- Public Methods
078:
079: private void log(Level level, String msg, Throwable ex) {
080: if (getLogger().isLoggable(level)) {
081: LogRecord record = new LogRecord(level, msg);
082: if (!classAndMethodFound) {
083: getClassAndMethod();
084: }
085: record.setSourceClassName(sourceClassName);
086: record.setSourceMethodName(sourceMethodName);
087: if (ex != null) {
088: record.setThrown(ex);
089: }
090: getLogger().log(record);
091: }
092: }
093:
094: /**
095: * <p>Gets the class and method by looking at the stack trace for the
096: * first entry that is not this class.</p>
097: */
098: private void getClassAndMethod() {
099: try {
100: Throwable throwable = new Throwable();
101: throwable.fillInStackTrace();
102: StringWriter stringWriter = new StringWriter();
103: PrintWriter printWriter = new PrintWriter(stringWriter);
104: throwable.printStackTrace(printWriter);
105: String traceString = stringWriter.getBuffer().toString();
106: StringTokenizer tokenizer = new StringTokenizer(
107: traceString, "\n");
108: tokenizer.nextToken();
109: String line = tokenizer.nextToken();
110: while (line.indexOf(this .getClass().getName()) == -1) {
111: line = tokenizer.nextToken();
112: }
113: while (line.indexOf(this .getClass().getName()) >= 0) {
114: line = tokenizer.nextToken();
115: }
116: int start = line.indexOf("at ") + 3;
117: int end = line.indexOf('(');
118: String temp = line.substring(start, end);
119: int lastPeriod = temp.lastIndexOf('.');
120: sourceClassName = temp.substring(0, lastPeriod);
121: sourceMethodName = temp.substring(lastPeriod + 1);
122: } catch (Exception ex) {
123: // ignore - leave class and methodname unknown
124: }
125: classAndMethodFound = true;
126: }
127:
128: /**
129: * Logs a message with <code>java.util.logging.Level.FINE</code>.
130: *
131: * @param message to log
132: * @see org.apache.commons.logging.Log#debug(Object)
133: */
134: public void debug(Object message) {
135: log(Level.FINE, String.valueOf(message), null);
136: }
137:
138: /**
139: * Logs a message with <code>java.util.logging.Level.FINE</code>.
140: *
141: * @param message to log
142: * @param exception log this cause
143: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
144: */
145: public void debug(Object message, Throwable exception) {
146: log(Level.FINE, String.valueOf(message), exception);
147: }
148:
149: /**
150: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
151: *
152: * @param message to log
153: * @see org.apache.commons.logging.Log#error(Object)
154: */
155: public void error(Object message) {
156: log(Level.SEVERE, String.valueOf(message), null);
157: }
158:
159: /**
160: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
161: *
162: * @param message to log
163: * @param exception log this cause
164: * @see org.apache.commons.logging.Log#error(Object, Throwable)
165: */
166: public void error(Object message, Throwable exception) {
167: log(Level.SEVERE, String.valueOf(message), exception);
168: }
169:
170: /**
171: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
172: *
173: * @param message to log
174: * @see org.apache.commons.logging.Log#fatal(Object)
175: */
176: public void fatal(Object message) {
177: log(Level.SEVERE, String.valueOf(message), null);
178: }
179:
180: /**
181: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
182: *
183: * @param message to log
184: * @param exception log this cause
185: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
186: */
187: public void fatal(Object message, Throwable exception) {
188: log(Level.SEVERE, String.valueOf(message), exception);
189: }
190:
191: /**
192: * Return the native Logger instance we are using.
193: */
194: public Logger getLogger() {
195: if (logger == null) {
196: logger = Logger.getLogger(name);
197: }
198: return (logger);
199: }
200:
201: /**
202: * Logs a message with <code>java.util.logging.Level.INFO</code>.
203: *
204: * @param message to log
205: * @see org.apache.commons.logging.Log#info(Object)
206: */
207: public void info(Object message) {
208: log(Level.INFO, String.valueOf(message), null);
209: }
210:
211: /**
212: * Logs a message with <code>java.util.logging.Level.INFO</code>.
213: *
214: * @param message to log
215: * @param exception log this cause
216: * @see org.apache.commons.logging.Log#info(Object, Throwable)
217: */
218: public void info(Object message, Throwable exception) {
219: log(Level.INFO, String.valueOf(message), exception);
220: }
221:
222: /**
223: * Is debug logging currently enabled?
224: */
225: public boolean isDebugEnabled() {
226: return (getLogger().isLoggable(Level.FINE));
227: }
228:
229: /**
230: * Is error logging currently enabled?
231: */
232: public boolean isErrorEnabled() {
233: return (getLogger().isLoggable(Level.SEVERE));
234: }
235:
236: /**
237: * Is fatal logging currently enabled?
238: */
239: public boolean isFatalEnabled() {
240: return (getLogger().isLoggable(Level.SEVERE));
241: }
242:
243: /**
244: * Is info logging currently enabled?
245: */
246: public boolean isInfoEnabled() {
247: return (getLogger().isLoggable(Level.INFO));
248: }
249:
250: /**
251: * Is trace logging currently enabled?
252: */
253: public boolean isTraceEnabled() {
254: return (getLogger().isLoggable(Level.FINEST));
255: }
256:
257: /**
258: * Is warn logging currently enabled?
259: */
260: public boolean isWarnEnabled() {
261: return (getLogger().isLoggable(Level.WARNING));
262: }
263:
264: /**
265: * Logs a message with <code>java.util.logging.Level.FINEST</code>.
266: *
267: * @param message to log
268: * @see org.apache.commons.logging.Log#trace(Object)
269: */
270: public void trace(Object message) {
271: log(Level.FINEST, String.valueOf(message), null);
272: }
273:
274: /**
275: * Logs a message with <code>java.util.logging.Level.FINEST</code>.
276: *
277: * @param message to log
278: * @param exception log this cause
279: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
280: */
281: public void trace(Object message, Throwable exception) {
282: log(Level.FINEST, String.valueOf(message), exception);
283: }
284:
285: /**
286: * Logs a message with <code>java.util.logging.Level.WARNING</code>.
287: *
288: * @param message to log
289: * @see org.apache.commons.logging.Log#warn(Object)
290: */
291: public void warn(Object message) {
292: log(Level.WARNING, String.valueOf(message), null);
293: }
294:
295: /**
296: * Logs a message with <code>java.util.logging.Level.WARNING</code>.
297: *
298: * @param message to log
299: * @param exception log this cause
300: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
301: */
302: public void warn(Object message, Throwable exception) {
303: log(Level.WARNING, String.valueOf(message), exception);
304: }
305:
306: }
|