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