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 import java.io.Serializable; 022 import org.apache.log.Logger; 023 import org.apache.log.Hierarchy; 024 import org.apache.commons.logging.Log; 025 026 /** 027 * <p>Implementation of <code>org.apache.commons.logging.Log</code> 028 * that wraps the <a href="http://avalon.apache.org/logkit/">avalon-logkit</a> 029 * logging system. Configuration of <code>LogKit</code> is left to the user. 030 * </p> 031 * 032 * <p><code>LogKit</code> accepts only <code>String</code> messages. 033 * Therefore, this implementation converts object messages into strings 034 * by called their <code>toString()</code> method before logging them.</p> 035 * 036 * @author <a href="mailto:sanders@apache.org">Scott Sanders</a> 037 * @author Robert Burrell Donkin 038 * @version $Id: LogKitLogger.java 424107 2006-07-20 23:15:42Z skitching $ 039 */ 040 041 public class LogKitLogger implements Log, Serializable { 042 043 044 // ------------------------------------------------------------- Attributes 045 046 047 /** Logging goes to this <code>LogKit</code> logger */ 048 protected transient Logger logger = null; 049 050 /** Name of this logger */ 051 protected String name = null; 052 053 054 // ------------------------------------------------------------ Constructor 055 056 057 /** 058 * Construct <code>LogKitLogger</code> which wraps the <code>LogKit</code> 059 * logger with given name. 060 * 061 * @param name log name 062 */ 063 public LogKitLogger(String name) { 064 this.name = name; 065 this.logger = getLogger(); 066 } 067 068 069 // --------------------------------------------------------- Public Methods 070 071 072 /** 073 * <p>Return the underlying Logger we are using.</p> 074 */ 075 public Logger getLogger() { 076 077 if (logger == null) { 078 logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name); 079 } 080 return (logger); 081 082 } 083 084 085 // ----------------------------------------------------- Log Implementation 086 087 088 /** 089 * Logs a message with <code>org.apache.log.Priority.DEBUG</code>. 090 * 091 * @param message to log 092 * @see org.apache.commons.logging.Log#trace(Object) 093 */ 094 public void trace(Object message) { 095 debug(message); 096 } 097 098 099 /** 100 * Logs a message with <code>org.apache.log.Priority.DEBUG</code>. 101 * 102 * @param message to log 103 * @param t log this cause 104 * @see org.apache.commons.logging.Log#trace(Object, Throwable) 105 */ 106 public void trace(Object message, Throwable t) { 107 debug(message, t); 108 } 109 110 111 /** 112 * Logs a message with <code>org.apache.log.Priority.DEBUG</code>. 113 * 114 * @param message to log 115 * @see org.apache.commons.logging.Log#debug(Object) 116 */ 117 public void debug(Object message) { 118 if (message != null) { 119 getLogger().debug(String.valueOf(message)); 120 } 121 } 122 123 124 /** 125 * Logs a message with <code>org.apache.log.Priority.DEBUG</code>. 126 * 127 * @param message to log 128 * @param t log this cause 129 * @see org.apache.commons.logging.Log#debug(Object, Throwable) 130 */ 131 public void debug(Object message, Throwable t) { 132 if (message != null) { 133 getLogger().debug(String.valueOf(message), t); 134 } 135 } 136 137 138 /** 139 * Logs a message with <code>org.apache.log.Priority.INFO</code>. 140 * 141 * @param message to log 142 * @see org.apache.commons.logging.Log#info(Object) 143 */ 144 public void info(Object message) { 145 if (message != null) { 146 getLogger().info(String.valueOf(message)); 147 } 148 } 149 150 151 /** 152 * Logs a message with <code>org.apache.log.Priority.INFO</code>. 153 * 154 * @param message to log 155 * @param t log this cause 156 * @see org.apache.commons.logging.Log#info(Object, Throwable) 157 */ 158 public void info(Object message, Throwable t) { 159 if (message != null) { 160 getLogger().info(String.valueOf(message), t); 161 } 162 } 163 164 165 /** 166 * Logs a message with <code>org.apache.log.Priority.WARN</code>. 167 * 168 * @param message to log 169 * @see org.apache.commons.logging.Log#warn(Object) 170 */ 171 public void warn(Object message) { 172 if (message != null) { 173 getLogger().warn(String.valueOf(message)); 174 } 175 } 176 177 178 /** 179 * Logs a message with <code>org.apache.log.Priority.WARN</code>. 180 * 181 * @param message to log 182 * @param t log this cause 183 * @see org.apache.commons.logging.Log#warn(Object, Throwable) 184 */ 185 public void warn(Object message, Throwable t) { 186 if (message != null) { 187 getLogger().warn(String.valueOf(message), t); 188 } 189 } 190 191 192 /** 193 * Logs a message with <code>org.apache.log.Priority.ERROR</code>. 194 * 195 * @param message to log 196 * @see org.apache.commons.logging.Log#error(Object) 197 */ 198 public void error(Object message) { 199 if (message != null) { 200 getLogger().error(String.valueOf(message)); 201 } 202 } 203 204 205 /** 206 * Logs a message with <code>org.apache.log.Priority.ERROR</code>. 207 * 208 * @param message to log 209 * @param t log this cause 210 * @see org.apache.commons.logging.Log#error(Object, Throwable) 211 */ 212 public void error(Object message, Throwable t) { 213 if (message != null) { 214 getLogger().error(String.valueOf(message), t); 215 } 216 } 217 218 219 /** 220 * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>. 221 * 222 * @param message to log 223 * @see org.apache.commons.logging.Log#fatal(Object) 224 */ 225 public void fatal(Object message) { 226 if (message != null) { 227 getLogger().fatalError(String.valueOf(message)); 228 } 229 } 230 231 232 /** 233 * Logs a message with <code>org.apache.log.Priority.FATAL_ERROR</code>. 234 * 235 * @param message to log 236 * @param t log this cause 237 * @see org.apache.commons.logging.Log#fatal(Object, Throwable) 238 */ 239 public void fatal(Object message, Throwable t) { 240 if (message != null) { 241 getLogger().fatalError(String.valueOf(message), t); 242 } 243 } 244 245 246 /** 247 * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>. 248 */ 249 public boolean isDebugEnabled() { 250 return getLogger().isDebugEnabled(); 251 } 252 253 254 /** 255 * Checks whether the <code>LogKit</code> logger will log messages of priority <code>ERROR</code>. 256 */ 257 public boolean isErrorEnabled() { 258 return getLogger().isErrorEnabled(); 259 } 260 261 262 /** 263 * Checks whether the <code>LogKit</code> logger will log messages of priority <code>FATAL_ERROR</code>. 264 */ 265 public boolean isFatalEnabled() { 266 return getLogger().isFatalErrorEnabled(); 267 } 268 269 270 /** 271 * Checks whether the <code>LogKit</code> logger will log messages of priority <code>INFO</code>. 272 */ 273 public boolean isInfoEnabled() { 274 return getLogger().isInfoEnabled(); 275 } 276 277 278 /** 279 * Checks whether the <code>LogKit</code> logger will log messages of priority <code>DEBUG</code>. 280 */ 281 public boolean isTraceEnabled() { 282 return getLogger().isDebugEnabled(); 283 } 284 285 286 /** 287 * Checks whether the <code>LogKit</code> logger will log messages of priority <code>WARN</code>. 288 */ 289 public boolean isWarnEnabled() { 290 return getLogger().isWarnEnabled(); 291 } 292 293 294 }