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 018package org.apache.commons.logging.impl; 019 020import java.io.Serializable; 021import java.util.logging.Level; 022import java.util.logging.Logger; 023 024import org.apache.commons.logging.Log; 025 026/** 027 * Implementation of the <code>org.apache.commons.logging.Log</code> 028 * interface that wraps the standard JDK logging mechanisms that were 029 * introduced in the Merlin release (JDK 1.4). 030 * 031 * @version $Id: Jdk14Logger.html 915605 2014-07-09 20:22:43Z tn $ 032 */ 033public class Jdk14Logger implements Log, Serializable { 034 035 /** Serializable version identifier. */ 036 private static final long serialVersionUID = 4784713551416303804L; 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 this.name = name; 055 logger = getLogger(); 056 } 057 058 // ----------------------------------------------------- Instance Variables 059 060 /** 061 * The underlying Logger implementation we are using. 062 */ 063 protected transient Logger logger = null; 064 065 /** 066 * The name of the logger we are wrapping. 067 */ 068 protected String name = null; 069 070 // --------------------------------------------------------- Protected Methods 071 072 protected void log( Level level, String msg, Throwable ex ) { 073 Logger logger = getLogger(); 074 if (logger.isLoggable(level)) { 075 // Hack (?) to get the stack trace. 076 Throwable dummyException = new Throwable(); 077 StackTraceElement locations[] = dummyException.getStackTrace(); 078 // LOGGING-132: use the provided logger name instead of the class name 079 String cname = name; 080 String method = "unknown"; 081 // Caller will be the third element 082 if( locations != null && locations.length > 2 ) { 083 StackTraceElement caller = locations[2]; 084 method = caller.getMethodName(); 085 } 086 if( ex == null ) { 087 logger.logp( level, cname, method, msg ); 088 } else { 089 logger.logp( level, cname, method, msg, ex ); 090 } 091 } 092 } 093 094 // --------------------------------------------------------- Public Methods 095 096 /** 097 * Logs a message with <code>java.util.logging.Level.FINE</code>. 098 * 099 * @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}