1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (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.mozilla.org/MPL/MPL-1.1.html 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 import org.as2lib.env.log.Logger; 18 import org.as2lib.env.log.LogLevel; 19 import org.as2lib.env.log.LogMessage; 20 import org.as2lib.env.log.logger.AbstractLogger; 21 22 import com.interactiveAlchemy.utils.Debug; 23 24 /** 25 * {@code DebugItLogger} logs messages using {@code com.interactiveAlchemy.utils.Debug.write} 26 * of the DebugIt Component from Robert Hoekman to log messages. 27 * 28 * <p>Note that this logger uses the {@link LogMessage} class to wrap and decorate log 29 * messages. 30 * 31 * @author Simon Wacker 32 * @see org.as2lib.env.log.handler.DebugItHandler 33 * @see <a href="http://www.rhjr.net/blog/2005/03/debugit-10.html">DebugIt Component</a> 34 */ 35 class org.as2lib.env.log.logger.DebugItLogger extends AbstractLogger implements Logger { 36 37 /** Makes the static variables of the super-class accessible through this class. */ 38 private static var __proto__:Function = AbstractLogger; 39 40 /** The set level. */ 41 private var level:LogLevel; 42 43 /** The set level as number. */ 44 private var levelAsNumber:Number; 45 46 /** The name of this logger. */ 47 private var name:String; 48 49 /** 50 * Constructs a new {@code DebugItLogger} instance. 51 * 52 * <p>The default log level is {@code ALL}. This means all messages regardless of 53 * their level are logged. 54 * 55 * <p>The {@code name} is by default shown in the log message to identify where 56 * the message came from. 57 * 58 * @param name (optional) the name of this logger 59 */ 60 public function DebugItLogger(name:String) { 61 this.name = name; 62 level = ALL; 63 levelAsNumber = level.toNumber(); 64 } 65 66 /** 67 * Returns the name of this logger. 68 * 69 * <p>This method returns {@code null} if no name has been set via the 70 * {@link #setName} method nor on construction. 71 * 72 * @return the name of this logger 73 */ 74 public function getName(Void):String { 75 return name; 76 } 77 78 /** 79 * Sets the name of this logger. 80 * 81 * <p>The name is by default shown in the log message. 82 * 83 * @param name the new name of this logger 84 */ 85 public function setName(name:String):Void { 86 this.name = name; 87 } 88 89 /** 90 * Sets the log level. 91 * 92 * <p>The log level determines which messages are logged and which are not. 93 * 94 * <p>A level of value {@code null} or {@code undefined} is interpreted as level 95 * {@code ALL} which is also the default level. 96 * 97 * @param level the new log level 98 */ 99 public function setLevel(level:LogLevel):Void { 100 if (level) { 101 this.level = level; 102 } else { 103 this.level = ALL; 104 } 105 this.levelAsNumber = this.level.toNumber(); 106 } 107 108 /** 109 * Returns the set level. 110 * 111 * @return the set level 112 */ 113 public function getLevel(Void):LogLevel { 114 return level; 115 } 116 117 /** 118 * Checks whether this logger is enabled for the passed-in {@code level}. 119 * 120 * <p>{@code false} will be returned if: 121 * <ul> 122 * <li>This logger is not enabled for the passed-in {@code level}.</li> 123 * <li>The passed-in {@code level} is {@code null} or {@code undefined}.</li> 124 * </ul> 125 * 126 * <p>Using this method as shown in the class documentation may improve performance 127 * depending on how long the log message construction takes. 128 * 129 * @param level the level to make the check upon 130 * @return {@code true} if this logger is enabled for the given {@code level} else 131 * {@code false} 132 * @see #log 133 */ 134 public function isEnabled(level:LogLevel):Boolean { 135 if (!level) return false; 136 return (levelAsNumber >= level.toNumber()); 137 } 138 139 /** 140 * Checks if this logger is enabled for debug level log messages. 141 * 142 * <p>Using this method as shown in the class documentation may improve performance 143 * depending on how long the log message construction takes. 144 * 145 * @return {@code true} if debug messages are logged 146 * @see org.as2lib.env.log.level.AbstractLogLevel#DEBUG 147 * @see #debug 148 */ 149 public function isDebugEnabled(Void):Boolean { 150 return (levelAsNumber >= debugLevelAsNumber); 151 } 152 153 /** 154 * Checks if this logger is enabled for info level log messages. 155 * 156 * <p>Using this method as shown in the class documentation may improve performance 157 * depending on how long the log message construction takes. 158 * 159 * @return {@code true} if info messages are logged 160 * @see org.as2lib.env.log.level.AbstractLogLevel#INFO 161 * @see #info 162 */ 163 public function isInfoEnabled(Void):Boolean { 164 return (levelAsNumber >= infoLevelAsNumber); 165 } 166 167 /** 168 * Checks if this logger is enabled for warning level log messages. 169 * 170 * <p>Using this method as shown in the class documentation may improve performance 171 * depending on how long the log message construction takes. 172 * 173 * @return {@code true} if warning messages are logged 174 * @see org.as2lib.env.log.level.AbstractLogLevel#WARNING 175 * @see #warning 176 */ 177 public function isWarningEnabled(Void):Boolean { 178 return (levelAsNumber >= warningLevelAsNumber); 179 } 180 181 /** 182 * Checks if this logger is enabled for error level log messages. 183 * 184 * <p>Using this method as shown in the class documentation may improve performance 185 * depending on how long the log message construction takes. 186 * 187 * @return {@code true} if error messages are logged 188 * @see org.as2lib.env.log.level.AbstractLogLevel#ERROR 189 * @see #error 190 */ 191 public function isErrorEnabled(Void):Boolean { 192 return (levelAsNumber >= errorLevelAsNumber); 193 } 194 195 /** 196 * Checks if this logger is enabled for fatal level log messages. 197 * 198 * <p>Using this method as shown in the class documentation may improve performance 199 * depending on how long the log message construction takes. 200 * 201 * @return {@code true} if fatal messages are logged 202 * @see org.as2lib.env.log.level.AbstractLogLevel#FATAL 203 * @see #fatal 204 */ 205 public function isFatalEnabled(Void):Boolean { 206 return (levelAsNumber >= fatalLevelAsNumber); 207 } 208 209 /** 210 * Logs the passed-in {@code message} at the given {@code level}. 211 * 212 * <p>The {@code message} is only logged when this logger is enabled for the 213 * passed-in {@code level}. 214 * 215 * @param message the message object to log 216 * @param level the specific level at which the {@code message} shall be logged 217 * @see #isEnabled 218 */ 219 public function log(message, level:LogLevel):Void { 220 if (isEnabled(level)) { 221 Debug.write((new LogMessage(message, level, name)).toString()); 222 } 223 } 224 225 /** 226 * Logs the passed-in {@code message} at debug level. 227 * 228 * <p>The {@code message} is only logged when the level is set to {@code DEBUG} or 229 * a level above. 230 * 231 * @param message the message object to log 232 * @see #isDebugEnabled 233 */ 234 public function debug(message):Void { 235 if (isDebugEnabled()) { 236 Debug.write((new LogMessage(message, debugLevel, name)).toString()); 237 } 238 } 239 240 /** 241 * Logs the passed-in {@code message} at info level. 242 * 243 * <p>The {@code message} is only logged when the level is set to {@code INFO} or 244 * a level above. 245 * 246 * @param message the message object to log 247 * @see #isInfoEnabled 248 */ 249 public function info(message):Void { 250 if (isInfoEnabled()) { 251 Debug.write((new LogMessage(message, infoLevel, name)).toString()); 252 } 253 } 254 255 /** 256 * Logs the passed-in {@code message} at warning level. 257 * 258 * <p>The {@code message} is only logged when the level is set to {@code WARNING} 259 * or a level above. 260 * 261 * @param message the message object to log 262 * @see #isWarningEnabled 263 */ 264 public function warning(message):Void { 265 if (isWarningEnabled()) { 266 Debug.write((new LogMessage(message, warningLevel, name)).toString()); 267 } 268 } 269 270 /** 271 * Logs the passed-in {@code message} at error level. 272 * 273 * <p>The {@code message} is only logged when the level is set to {@code ERROR} or 274 * a level above. 275 * 276 * @param message the message object to log 277 * @see #isErrorEnabled 278 */ 279 public function error(message):Void { 280 if (isErrorEnabled()) { 281 Debug.write((new LogMessage(message, errorLevel, name)).toString()); 282 } 283 } 284 285 /** 286 * Logs the passed-in {@code message} at fatal level. 287 * 288 * <p>The {@code message} is only logged when the level is set to {@code FATAL} or 289 * a level above. 290 * 291 * @param message the message object to log 292 * @see #isFatalEnabled 293 */ 294 public function fatal(message):Void { 295 if (isFatalEnabled()) { 296 Debug.write((new LogMessage(message, fatalLevel, name)).toString()); 297 } 298 } 299 300 }