View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  
18  package org.apache.commons.logging.impl;
19  
20  
21  import java.io.Serializable;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import org.apache.commons.logging.Log;
26  
27  
28  /***
29   * <p>Implementation of the <code>org.apache.commons.logging.Log</code>
30   * interface that wraps the standard JDK logging mechanisms that were
31   * introduced in the Merlin release (JDK 1.4).</p>
32   *
33   * @author <a href="mailto:sanders@apache.org">Scott Sanders</a>
34   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
35   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
36   * @version $Revision: 1.13 $ $Date: 2004/06/06 21:10:21 $
37   */
38  
39  public class Jdk14Logger implements Log, Serializable {
40  
41  
42      // ----------------------------------------------------------- Constructors
43  
44  
45      /***
46       * Construct a named instance of this Logger.
47       *
48       * @param name Name of the logger to be constructed
49       */
50      public Jdk14Logger(String name) {
51  
52          this.name = name;
53          logger = getLogger();
54  
55      }
56  
57  
58      // ----------------------------------------------------- Instance Variables
59  
60  
61      /***
62       * The underlying Logger implementation we are using.
63       */
64      protected transient Logger logger = null;
65  
66  
67      /***
68       * The name of the logger we are wrapping.
69       */
70      protected String name = null;
71  
72  
73      // --------------------------------------------------------- Public Methods
74  
75      private void log( Level level, String msg, Throwable ex ) {
76  
77          Logger logger = getLogger();
78          if (logger.isLoggable(level)) {
79              // Hack (?) to get the stack trace.
80              Throwable dummyException=new Throwable();
81              StackTraceElement locations[]=dummyException.getStackTrace();
82              // Caller will be the third element
83              String cname="unknown";
84              String method="unknown";
85              if( locations!=null && locations.length >2 ) {
86                  StackTraceElement caller=locations[2];
87                  cname=caller.getClassName();
88                  method=caller.getMethodName();
89              }
90              if( ex==null ) {
91                  logger.logp( level, cname, method, msg );
92              } else {
93                  logger.logp( level, cname, method, msg, ex );
94              }
95          }
96  
97      }
98  
99      /***
100      * Log a message with debug log level.
101      */
102     public void debug(Object message) {
103         log(Level.FINE, String.valueOf(message), null);
104     }
105 
106 
107     /***
108      * Log a message and exception with debug log level.
109      */
110     public void debug(Object message, Throwable exception) {
111         log(Level.FINE, String.valueOf(message), exception);
112     }
113 
114 
115     /***
116      * Log a message with error log level.
117      */
118     public void error(Object message) {
119         log(Level.SEVERE, String.valueOf(message), null);
120     }
121 
122 
123     /***
124      * Log a message and exception with error log level.
125      */
126     public void error(Object message, Throwable exception) {
127         log(Level.SEVERE, String.valueOf(message), exception);
128     }
129 
130 
131     /***
132      * Log a message with fatal log level.
133      */
134     public void fatal(Object message) {
135         log(Level.SEVERE, String.valueOf(message), null);
136     }
137 
138 
139     /***
140      * Log a message and exception with fatal log level.
141      */
142     public void fatal(Object message, Throwable exception) {
143         log(Level.SEVERE, String.valueOf(message), exception);
144     }
145 
146 
147     /***
148      * Return the native Logger instance we are using.
149      */
150     public Logger getLogger() {
151         if (logger == null) {
152             logger = Logger.getLogger(name);
153         }
154         return (logger);
155     }
156 
157 
158     /***
159      * Log a message with info log level.
160      */
161     public void info(Object message) {
162         log(Level.INFO, String.valueOf(message), null);
163     }
164 
165 
166     /***
167      * Log a message and exception with info log level.
168      */
169     public void info(Object message, Throwable exception) {
170         log(Level.INFO, String.valueOf(message), exception);
171     }
172 
173 
174     /***
175      * Is debug logging currently enabled?
176      */
177     public boolean isDebugEnabled() {
178         return (getLogger().isLoggable(Level.FINE));
179     }
180 
181 
182     /***
183      * Is error logging currently enabled?
184      */
185     public boolean isErrorEnabled() {
186         return (getLogger().isLoggable(Level.SEVERE));
187     }
188 
189 
190     /***
191      * Is fatal logging currently enabled?
192      */
193     public boolean isFatalEnabled() {
194         return (getLogger().isLoggable(Level.SEVERE));
195     }
196 
197 
198     /***
199      * Is info logging currently enabled?
200      */
201     public boolean isInfoEnabled() {
202         return (getLogger().isLoggable(Level.INFO));
203     }
204 
205 
206     /***
207      * Is trace logging currently enabled?
208      */
209     public boolean isTraceEnabled() {
210         return (getLogger().isLoggable(Level.FINEST));
211     }
212 
213 
214     /***
215      * Is warn logging currently enabled?
216      */
217     public boolean isWarnEnabled() {
218         return (getLogger().isLoggable(Level.WARNING));
219     }
220 
221 
222     /***
223      * Log a message with trace log level.
224      */
225     public void trace(Object message) {
226         log(Level.FINEST, String.valueOf(message), null);
227     }
228 
229 
230     /***
231      * Log a message and exception with trace log level.
232      */
233     public void trace(Object message, Throwable exception) {
234         log(Level.FINEST, String.valueOf(message), exception);
235     }
236 
237 
238     /***
239      * Log a message with warn log level.
240      */
241     public void warn(Object message) {
242         log(Level.WARNING, String.valueOf(message), null);
243     }
244 
245 
246     /***
247      * Log a message and exception with warn log level.
248      */
249     public void warn(Object message, Throwable exception) {
250         log(Level.WARNING, String.valueOf(message), exception);
251     }
252 
253 
254 }