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