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 * https://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 * Implements the {@code org.apache.commons.logging.Log}
28 * interface to wrap the standard JDK logging mechanisms that were
29 * introduced in the Merlin release (JDK 1.4).
30 */
31 public class Jdk14Logger implements Log, Serializable {
32
33 /** Serializable version identifier. */
34 private static final long serialVersionUID = 4784713551416303804L;
35
36 /**
37 * This member variable simply ensures that any attempt to initialize
38 * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
39 * It must not be private, as an optimizing compiler could detect that it
40 * is not used and optimize it away.
41 */
42 protected static final Level dummyLevel = Level.FINE;
43
44 /**
45 * The underlying Logger implementation we are using.
46 */
47 protected transient Logger logger;
48
49 /**
50 * The name of the logger we are wrapping.
51 */
52 protected String name;
53
54 /**
55 * Constructs a named instance of this Logger.
56 *
57 * @param name Name of the logger to be constructed
58 */
59 public Jdk14Logger(final String name) {
60 this.name = name;
61 logger = getLogger();
62 }
63
64 /**
65 * Logs a message with {@link java.util.logging.Level#FINE}.
66 *
67 * @param message to log
68 * @see org.apache.commons.logging.Log#debug(Object)
69 */
70 @Override
71 public void debug(final Object message) {
72 log(Level.FINE, String.valueOf(message), null);
73 }
74
75 /**
76 * Logs a message with {@link java.util.logging.Level#FINE}.
77 *
78 * @param message to log
79 * @param exception log this cause
80 * @see org.apache.commons.logging.Log#debug(Object, Throwable)
81 */
82 @Override
83 public void debug(final Object message, final Throwable exception) {
84 log(Level.FINE, String.valueOf(message), exception);
85 }
86
87 /**
88 * Logs a message with {@link java.util.logging.Level#SEVERE}.
89 *
90 * @param message to log
91 * @see org.apache.commons.logging.Log#error(Object)
92 */
93 @Override
94 public void error(final Object message) {
95 log(Level.SEVERE, String.valueOf(message), null);
96 }
97
98 /**
99 * Logs a message with {@link java.util.logging.Level#SEVERE}.
100 *
101 * @param message to log
102 * @param exception log this cause
103 * @see org.apache.commons.logging.Log#error(Object, Throwable)
104 */
105 @Override
106 public void error(final Object message, final Throwable exception) {
107 log(Level.SEVERE, String.valueOf(message), exception);
108 }
109
110 /**
111 * Logs a message with {@link java.util.logging.Level#SEVERE}.
112 *
113 * @param message to log
114 * @see org.apache.commons.logging.Log#fatal(Object)
115 */
116 @Override
117 public void fatal(final Object message) {
118 log(Level.SEVERE, String.valueOf(message), null);
119 }
120
121 /**
122 * Logs a message with {@link java.util.logging.Level#SEVERE}.
123 *
124 * @param message to log
125 * @param exception log this cause
126 * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
127 */
128 @Override
129 public void fatal(final Object message, final Throwable exception) {
130 log(Level.SEVERE, String.valueOf(message), exception);
131 }
132
133 /**
134 * Gets the native Logger instance we are using.
135 *
136 * @return the native Logger instance we are using.
137 */
138 public Logger getLogger() {
139 if (logger == null) {
140 logger = Logger.getLogger(name);
141 }
142 return logger;
143 }
144
145 /**
146 * Logs a message with {@link java.util.logging.Level#INFO}.
147 *
148 * @param message to log
149 * @see org.apache.commons.logging.Log#info(Object)
150 */
151 @Override
152 public void info(final Object message) {
153 log(Level.INFO, String.valueOf(message), null);
154 }
155
156 /**
157 * Logs a message with {@link java.util.logging.Level#INFO}.
158 *
159 * @param message to log
160 * @param exception log this cause
161 * @see org.apache.commons.logging.Log#info(Object, Throwable)
162 */
163 @Override
164 public void info(final Object message, final Throwable exception) {
165 log(Level.INFO, String.valueOf(message), exception);
166 }
167
168 /**
169 * Is debug logging currently enabled?
170 */
171 @Override
172 public boolean isDebugEnabled() {
173 return getLogger().isLoggable(Level.FINE);
174 }
175
176 /**
177 * Is error logging currently enabled?
178 */
179 @Override
180 public boolean isErrorEnabled() {
181 return getLogger().isLoggable(Level.SEVERE);
182 }
183
184 /**
185 * Is fatal logging currently enabled?
186 */
187 @Override
188 public boolean isFatalEnabled() {
189 return getLogger().isLoggable(Level.SEVERE);
190 }
191
192 /**
193 * Is info logging currently enabled?
194 */
195 @Override
196 public boolean isInfoEnabled() {
197 return getLogger().isLoggable(Level.INFO);
198 }
199
200 /**
201 * Is trace logging currently enabled?
202 */
203 @Override
204 public boolean isTraceEnabled() {
205 return getLogger().isLoggable(Level.FINEST);
206 }
207
208 /**
209 * Is warn logging currently enabled?
210 */
211 @Override
212 public boolean isWarnEnabled() {
213 return getLogger().isLoggable(Level.WARNING);
214 }
215
216 /**
217 * Logs a message at the given level.
218 *
219 * @param level The level.
220 * @param msg The message.
221 * @param ex The exception.
222 */
223 protected void log(final Level level, final String msg, final Throwable ex) {
224 final Logger logger = getLogger();
225 if (logger.isLoggable(level)) {
226 // Hack (?) to get the stack trace.
227 final Throwable dummyException = new Throwable();
228 final StackTraceElement[] locations = dummyException.getStackTrace();
229 // LOGGING-132: use the provided logger name instead of the class name
230 final String cname = name;
231 String method = "unknown";
232 // Caller will be the third element
233 if (locations != null && locations.length > 2) {
234 final StackTraceElement caller = locations[2];
235 method = caller.getMethodName();
236 }
237 if (ex == null) {
238 logger.logp(level, cname, method, msg);
239 } else {
240 logger.logp(level, cname, method, msg, ex);
241 }
242 }
243 }
244
245 /**
246 * Logs a message with {@link java.util.logging.Level#FINEST}.
247 *
248 * @param message to log
249 * @see org.apache.commons.logging.Log#trace(Object)
250 */
251 @Override
252 public void trace(final Object message) {
253 log(Level.FINEST, String.valueOf(message), null);
254 }
255
256 /**
257 * Logs a message with {@link java.util.logging.Level#FINEST}.
258 *
259 * @param message to log
260 * @param exception log this cause
261 * @see org.apache.commons.logging.Log#trace(Object, Throwable)
262 */
263 @Override
264 public void trace(final Object message, final Throwable exception) {
265 log(Level.FINEST, String.valueOf(message), exception);
266 }
267
268 /**
269 * Logs a message with {@link java.util.logging.Level#WARNING}.
270 *
271 * @param message to log
272 * @see org.apache.commons.logging.Log#warn(Object)
273 */
274 @Override
275 public void warn(final Object message) {
276 log(Level.WARNING, String.valueOf(message), null);
277 }
278
279 /**
280 * Logs a message with {@link java.util.logging.Level#WARNING}.
281 *
282 * @param message to log
283 * @param exception log this cause
284 * @see org.apache.commons.logging.Log#warn(Object, Throwable)
285 */
286 @Override
287 public void warn(final Object message, final Throwable exception) {
288 log(Level.WARNING, String.valueOf(message), exception);
289 }
290 }