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:
023: import org.apache.commons.logging.Log;
024:
025: /**
026: * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
027: * interface that wraps the standard JDK logging mechanisms that were
028: * introduced in the Merlin release (JDK 1.4).</p>
029: *
030: * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
031: * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
032: * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
033: * @version $Revision: 370652 $ $Date: 2006-01-19 22:23:48 +0000 (Thu, 19 Jan 2006) $
034: */
035:
036: public class Jdk14Logger implements Log, Serializable {
037:
038: /**
039: * This member variable simply ensures that any attempt to initialise
040: * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
041: * It must not be private, as an optimising compiler could detect that it
042: * is not used and optimise it away.
043: */
044: protected static final Level dummyLevel = Level.FINE;
045:
046: // ----------------------------------------------------------- Constructors
047:
048: /**
049: * Construct a named instance of this Logger.
050: *
051: * @param name Name of the logger to be constructed
052: */
053: public Jdk14Logger(String name) {
054:
055: this .name = name;
056: logger = getLogger();
057:
058: }
059:
060: // ----------------------------------------------------- Instance Variables
061:
062: /**
063: * The underlying Logger implementation we are using.
064: */
065: protected transient Logger logger = null;
066:
067: /**
068: * The name of the logger we are wrapping.
069: */
070: protected String name = null;
071:
072: // --------------------------------------------------------- Public Methods
073:
074: private void log(Level level, String msg, Throwable ex) {
075:
076: Logger logger = getLogger();
077: if (logger.isLoggable(level)) {
078: // Hack (?) to get the stack trace.
079: Throwable dummyException = new Throwable();
080: StackTraceElement locations[] = dummyException
081: .getStackTrace();
082: // Caller will be the third element
083: String cname = "unknown";
084: String method = "unknown";
085: if (locations != null && locations.length > 2) {
086: StackTraceElement caller = locations[2];
087: cname = caller.getClassName();
088: method = caller.getMethodName();
089: }
090: if (ex == null) {
091: logger.logp(level, cname, method, msg);
092: } else {
093: logger.logp(level, cname, method, msg, ex);
094: }
095: }
096:
097: }
098:
099: /**
100: * Logs a message with <code>java.util.logging.Level.FINE</code>.
101: *
102: * @param message to log
103: * @see org.apache.commons.logging.Log#debug(Object)
104: */
105: public void debug(Object message) {
106: log(Level.FINE, String.valueOf(message), null);
107: }
108:
109: /**
110: * Logs a message with <code>java.util.logging.Level.FINE</code>.
111: *
112: * @param message to log
113: * @param exception log this cause
114: * @see org.apache.commons.logging.Log#debug(Object, Throwable)
115: */
116: public void debug(Object message, Throwable exception) {
117: log(Level.FINE, String.valueOf(message), exception);
118: }
119:
120: /**
121: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
122: *
123: * @param message to log
124: * @see org.apache.commons.logging.Log#error(Object)
125: */
126: public void error(Object message) {
127: log(Level.SEVERE, String.valueOf(message), null);
128: }
129:
130: /**
131: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
132: *
133: * @param message to log
134: * @param exception log this cause
135: * @see org.apache.commons.logging.Log#error(Object, Throwable)
136: */
137: public void error(Object message, Throwable exception) {
138: log(Level.SEVERE, String.valueOf(message), exception);
139: }
140:
141: /**
142: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
143: *
144: * @param message to log
145: * @see org.apache.commons.logging.Log#fatal(Object)
146: */
147: public void fatal(Object message) {
148: log(Level.SEVERE, String.valueOf(message), null);
149: }
150:
151: /**
152: * Logs a message with <code>java.util.logging.Level.SEVERE</code>.
153: *
154: * @param message to log
155: * @param exception log this cause
156: * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
157: */
158: public void fatal(Object message, Throwable exception) {
159: log(Level.SEVERE, String.valueOf(message), exception);
160: }
161:
162: /**
163: * Return the native Logger instance we are using.
164: */
165: public Logger getLogger() {
166: if (logger == null) {
167: logger = Logger.getLogger(name);
168: }
169: return (logger);
170: }
171:
172: /**
173: * Logs a message with <code>java.util.logging.Level.INFO</code>.
174: *
175: * @param message to log
176: * @see org.apache.commons.logging.Log#info(Object)
177: */
178: public void info(Object message) {
179: log(Level.INFO, String.valueOf(message), null);
180: }
181:
182: /**
183: * Logs a message with <code>java.util.logging.Level.INFO</code>.
184: *
185: * @param message to log
186: * @param exception log this cause
187: * @see org.apache.commons.logging.Log#info(Object, Throwable)
188: */
189: public void info(Object message, Throwable exception) {
190: log(Level.INFO, String.valueOf(message), exception);
191: }
192:
193: /**
194: * Is debug logging currently enabled?
195: */
196: public boolean isDebugEnabled() {
197: return (getLogger().isLoggable(Level.FINE));
198: }
199:
200: /**
201: * Is error logging currently enabled?
202: */
203: public boolean isErrorEnabled() {
204: return (getLogger().isLoggable(Level.SEVERE));
205: }
206:
207: /**
208: * Is fatal logging currently enabled?
209: */
210: public boolean isFatalEnabled() {
211: return (getLogger().isLoggable(Level.SEVERE));
212: }
213:
214: /**
215: * Is info logging currently enabled?
216: */
217: public boolean isInfoEnabled() {
218: return (getLogger().isLoggable(Level.INFO));
219: }
220:
221: /**
222: * Is trace logging currently enabled?
223: */
224: public boolean isTraceEnabled() {
225: return (getLogger().isLoggable(Level.FINEST));
226: }
227:
228: /**
229: * Is warn logging currently enabled?
230: */
231: public boolean isWarnEnabled() {
232: return (getLogger().isLoggable(Level.WARNING));
233: }
234:
235: /**
236: * Logs a message with <code>java.util.logging.Level.FINEST</code>.
237: *
238: * @param message to log
239: * @see org.apache.commons.logging.Log#trace(Object)
240: */
241: public void trace(Object message) {
242: log(Level.FINEST, String.valueOf(message), null);
243: }
244:
245: /**
246: * Logs a message with <code>java.util.logging.Level.FINEST</code>.
247: *
248: * @param message to log
249: * @param exception log this cause
250: * @see org.apache.commons.logging.Log#trace(Object, Throwable)
251: */
252: public void trace(Object message, Throwable exception) {
253: log(Level.FINEST, String.valueOf(message), exception);
254: }
255:
256: /**
257: * Logs a message with <code>java.util.logging.Level.WARNING</code>.
258: *
259: * @param message to log
260: * @see org.apache.commons.logging.Log#warn(Object)
261: */
262: public void warn(Object message) {
263: log(Level.WARNING, String.valueOf(message), null);
264: }
265:
266: /**
267: * Logs a message with <code>java.util.logging.Level.WARNING</code>.
268: *
269: * @param message to log
270: * @param exception log this cause
271: * @see org.apache.commons.logging.Log#warn(Object, Throwable)
272: */
273: public void warn(Object message, Throwable exception) {
274: log(Level.WARNING, String.valueOf(message), exception);
275: }
276:
277: }
|