/brightaction/src/com/brightcove/brightaction/utils/Logger.as

http://github.com/BrightcoveOS/BrightAction · 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. */
  19. package com.brightcove.brightaction.utils {
  20. import flash.utils.describeType;
  21. /**
  22. * Flash Log trace logging utility
  23. *
  24. * @author amanning
  25. */
  26. public class Logger {
  27. /** The label prepended to all BrightAction Log mesages */
  28. private static const PREPEND_LABEL:String = "BrightAction Log >> ";
  29. /** The types of messages to actually log- null or empty Array means logging is disabled */
  30. private static var _logLevelList:Array = [
  31. LogTypeEnum.ERROR,
  32. LogTypeEnum.INFO,
  33. LogTypeEnum.WARN,
  34. LogTypeEnum.DEBUG];
  35. /** Return the current Log Level */
  36. public static function getLogLevel():Array {
  37. return _logLevelList;
  38. }
  39. /** Turn on all Log levels */
  40. public static function enableAllLogging():void {
  41. info("enableAllLogging");
  42. _logLevelList = [
  43. LogTypeEnum.ERROR,
  44. LogTypeEnum.INFO,
  45. LogTypeEnum.WARN,
  46. LogTypeEnum.DEBUG];
  47. }
  48. /** Turn off all Log levels */
  49. public static function disableAllLogging():void {
  50. info("disableAllLogging");
  51. _logLevelList = [];
  52. }
  53. /**
  54. * Set testing log level
  55. *
  56. * @param levelList list of levels that should be logged
  57. */
  58. public static function setLogLevel(list:Array):void {
  59. info("setLogLevel");
  60. if (list == null) {
  61. _logLevelList = [];
  62. }
  63. for each (var level:Object in list) {
  64. debug(
  65. "level to set: {0}, is LogTypeEnum: {1}",
  66. Logger,
  67. level,
  68. (level is LogTypeEnum));
  69. debug("level to set: {0}", Logger, level);
  70. if (!(level is LogTypeEnum)) {
  71. throw new Error("Invalid Log Level");
  72. }
  73. }
  74. _logLevelList = list;
  75. }
  76. /**
  77. * Output the log. Ignore if no length
  78. *
  79. * @param message the message to log
  80. */
  81. private static function outputLog(message:String):void {
  82. if (message.length > 0) {
  83. trace(message);
  84. }
  85. }
  86. /**
  87. * Log trace string in an Debug condition
  88. *
  89. * @param log the warn string to log
  90. * @param source the object logging this information, optional
  91. * @param rest array of string replacement params
  92. */
  93. public static function debug(log:String, source:Object = null, ... rest):void {
  94. outputLog(buildLog(LogTypeEnum.DEBUG, log, source, rest));
  95. }
  96. /**
  97. * Log trace string in an Info condition
  98. *
  99. * @param log the warn string to log
  100. * @param source the object logging this information, optional
  101. * @param rest array of string replacement params
  102. */
  103. public static function info(log:String, source:Object = null, ... rest):void {
  104. outputLog(buildLog(LogTypeEnum.INFO, log, source, rest));
  105. }
  106. /**
  107. * Log trace string in an Warn condition
  108. *
  109. * @param log the warn string to log
  110. * @param source the object logging this information, optional
  111. * @param rest array of string replacement params
  112. */
  113. public static function warn(log:String, source:Object = null, ... rest):void {
  114. outputLog(buildLog(LogTypeEnum.WARN, log, source, rest));
  115. }
  116. /**
  117. * Log trace string in an Error condition
  118. *
  119. * @param log the error string to log
  120. * @param source the object logging this information, optional
  121. * @param rest array of string replacement params
  122. */
  123. public static function error(log:String, source:Object = null, ... rest):void {
  124. outputLog(buildLog(LogTypeEnum.ERROR, log, source, rest));
  125. }
  126. /**
  127. * Depending on the type of log, and the log itself, return the correct logging string
  128. *
  129. * @param type the type of log message
  130. * @param message the log message
  131. * @param source the object logging this information, optional
  132. * @return the complete string to be logged, depending on the log level- empty
  133. * string if this log level is not enabled
  134. */
  135. private static function buildLog(
  136. type:LogTypeEnum,
  137. message:String,
  138. source:Object = null,
  139. ... rest):String {
  140. var result:String = '';
  141. var clazz:String = "unknown";
  142. if (source != null) {
  143. var typeList:Array = describeType(source).@name.split("::");
  144. clazz = typeList[1];
  145. }
  146. if (_logLevelList == null) {
  147. return result;
  148. }
  149. if (_logLevelList.indexOf(type) != -1) {
  150. message = (rest.length == 1) ? StringUtils.substitute(message, rest[0]) : message;
  151. result = PREPEND_LABEL + "[" + type.value + "]" + "[" + clazz + "] " + message;
  152. }
  153. return result;
  154. }
  155. /**
  156. * Generate a Log trace string in an Error condition
  157. *
  158. * @param log the error string to log
  159. * @param source the object logging this information, optional
  160. * @param rest array of string replacement params
  161. */
  162. public static function buildError(log:String, source:Object = null, ... rest):String {
  163. return buildLog(LogTypeEnum.ERROR, log, source);
  164. }
  165. /**
  166. * Generate a Log trace string in an Warn condition
  167. *
  168. * @param log the warn string to log
  169. * @param source the object logging this information, optional
  170. * @param rest array of string replacement params
  171. */
  172. public static function buildWarn(log:String, source:Object = null, ... rest):String {
  173. return buildLog(LogTypeEnum.WARN, log, source);
  174. }
  175. /**
  176. * Generate a Log trace string in an Warn condition
  177. *
  178. * @param log the warn string to log
  179. * @param source the object logging this information, optional
  180. * @param rest array of string replacement params
  181. */
  182. public static function buildInfo(log:String, source:Object = null, ... rest):String {
  183. return buildLog(LogTypeEnum.INFO, log, source, rest);
  184. }
  185. /**
  186. * Generate a Log trace string in an Debug condition
  187. *
  188. * @param log the debug string to log
  189. * @param source the object logging this information, optional
  190. * @param rest array of string replacement params
  191. */
  192. public static function buildDebug(log:String, source:Object = null, ... rest):String {
  193. return buildLog(LogTypeEnum.DEBUG, log, source, rest);
  194. }
  195. }
  196. }