/brightaction/src/com/brightcove/brightaction/utils/Logger.as
ActionScript | 261 lines | 93 code | 65 blank | 103 comment | 12 complexity | 1500d5355326cad1ec13c7f5352a1a9b MD5 | raw file
1/** 2 * 3 * GNU General Public License v3 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17 * THE SOFTWARE. 18 */ 19package com.brightcove.brightaction.utils { 20 21 import flash.utils.describeType; 22 23 /** 24 * Flash Log trace logging utility 25 * 26 * @author amanning 27 */ 28 public class Logger { 29 30 /** The label prepended to all BrightAction Log mesages */ 31 private static const PREPEND_LABEL:String = "BrightAction Log >> "; 32 33 /** The types of messages to actually log- null or empty Array means logging is disabled */ 34 private static var _logLevelList:Array = [ 35 LogTypeEnum.ERROR, 36 LogTypeEnum.INFO, 37 LogTypeEnum.WARN, 38 LogTypeEnum.DEBUG]; 39 40 /** Return the current Log Level */ 41 public static function getLogLevel():Array { 42 43 return _logLevelList; 44 45 } 46 47 /** Turn on all Log levels */ 48 public static function enableAllLogging():void { 49 50 info("enableAllLogging"); 51 52 _logLevelList = [ 53 LogTypeEnum.ERROR, 54 LogTypeEnum.INFO, 55 LogTypeEnum.WARN, 56 LogTypeEnum.DEBUG]; 57 58 } 59 60 /** Turn off all Log levels */ 61 public static function disableAllLogging():void { 62 63 info("disableAllLogging"); 64 65 _logLevelList = []; 66 67 } 68 69 /** 70 * Set testing log level 71 * 72 * @param levelList list of levels that should be logged 73 */ 74 public static function setLogLevel(list:Array):void { 75 76 info("setLogLevel"); 77 78 if (list == null) { 79 _logLevelList = []; 80 } 81 82 for each (var level:Object in list) { 83 84 debug( 85 "level to set: {0}, is LogTypeEnum: {1}", 86 Logger, 87 level, 88 (level is LogTypeEnum)); 89 90 debug("level to set: {0}", Logger, level); 91 92 if (!(level is LogTypeEnum)) { 93 throw new Error("Invalid Log Level"); 94 } 95 } 96 97 _logLevelList = list; 98 99 } 100 101 /** 102 * Output the log. Ignore if no length 103 * 104 * @param message the message to log 105 */ 106 private static function outputLog(message:String):void { 107 108 if (message.length > 0) { 109 trace(message); 110 } 111 112 } 113 114 /** 115 * Log trace string in an Debug condition 116 * 117 * @param log the warn string to log 118 * @param source the object logging this information, optional 119 * @param rest array of string replacement params 120 */ 121 public static function debug(log:String, source:Object = null, ... rest):void { 122 123 outputLog(buildLog(LogTypeEnum.DEBUG, log, source, rest)); 124 125 } 126 127 /** 128 * Log trace string in an Info condition 129 * 130 * @param log the warn string to log 131 * @param source the object logging this information, optional 132 * @param rest array of string replacement params 133 */ 134 public static function info(log:String, source:Object = null, ... rest):void { 135 136 outputLog(buildLog(LogTypeEnum.INFO, log, source, rest)); 137 138 } 139 140 /** 141 * Log trace string in an Warn condition 142 * 143 * @param log the warn string to log 144 * @param source the object logging this information, optional 145 * @param rest array of string replacement params 146 */ 147 public static function warn(log:String, source:Object = null, ... rest):void { 148 149 outputLog(buildLog(LogTypeEnum.WARN, log, source, rest)); 150 151 } 152 153 /** 154 * Log trace string in an Error condition 155 * 156 * @param log the error string to log 157 * @param source the object logging this information, optional 158 * @param rest array of string replacement params 159 */ 160 public static function error(log:String, source:Object = null, ... rest):void { 161 162 outputLog(buildLog(LogTypeEnum.ERROR, log, source, rest)); 163 164 } 165 166 /** 167 * Depending on the type of log, and the log itself, return the correct logging string 168 * 169 * @param type the type of log message 170 * @param message the log message 171 * @param source the object logging this information, optional 172 * @return the complete string to be logged, depending on the log level- empty 173 * string if this log level is not enabled 174 */ 175 private static function buildLog( 176 type:LogTypeEnum, 177 message:String, 178 source:Object = null, 179 ... rest):String { 180 181 var result:String = ''; 182 183 var clazz:String = "unknown"; 184 if (source != null) { 185 var typeList:Array = describeType(source).@name.split("::"); 186 187 clazz = typeList[1]; 188 189 } 190 191 if (_logLevelList == null) { 192 return result; 193 } 194 195 if (_logLevelList.indexOf(type) != -1) { 196 197 message = (rest.length == 1) ? StringUtils.substitute(message, rest[0]) : message; 198 result = PREPEND_LABEL + "[" + type.value + "]" + "[" + clazz + "] " + message; 199 200 } 201 202 return result; 203 204 } 205 206 /** 207 * Generate a Log trace string in an Error condition 208 * 209 * @param log the error string to log 210 * @param source the object logging this information, optional 211 * @param rest array of string replacement params 212 */ 213 public static function buildError(log:String, source:Object = null, ... rest):String { 214 215 return buildLog(LogTypeEnum.ERROR, log, source); 216 217 } 218 219 /** 220 * Generate a Log trace string in an Warn condition 221 * 222 * @param log the warn string to log 223 * @param source the object logging this information, optional 224 * @param rest array of string replacement params 225 */ 226 public static function buildWarn(log:String, source:Object = null, ... rest):String { 227 228 return buildLog(LogTypeEnum.WARN, log, source); 229 230 } 231 232 /** 233 * Generate a Log trace string in an Warn condition 234 * 235 * @param log the warn string to log 236 * @param source the object logging this information, optional 237 * @param rest array of string replacement params 238 */ 239 public static function buildInfo(log:String, source:Object = null, ... rest):String { 240 241 return buildLog(LogTypeEnum.INFO, log, source, rest); 242 243 244 } 245 246 /** 247 * Generate a Log trace string in an Debug condition 248 * 249 * @param log the debug string to log 250 * @param source the object logging this information, optional 251 * @param rest array of string replacement params 252 */ 253 public static function buildDebug(log:String, source:Object = null, ... rest):String { 254 255 return buildLog(LogTypeEnum.DEBUG, log, source, rest); 256 257 } 258 259 } 260 261}