PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.0/log5f/src/org/log5f/LoggerManager.as

http://log5f.googlecode.com/
ActionScript | 325 lines | 122 code | 45 blank | 158 comment | 23 complexity | 6876e06f3d06cc60a6be8c6e4bbd8c44 MD5 | raw file
Possible License(s): Apache-2.0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) 2009 http://log5f.org
  3. // This program is made available under the terms of the MIT License.
  4. ////////////////////////////////////////////////////////////////////////////////
  5. package org.log5f
  6. {
  7. import flash.utils.describeType;
  8. import org.log5f.appenders.ArthropodAppender;
  9. import org.log5f.appenders.BrowserAppender;
  10. import org.log5f.appenders.FirebugAppender;
  11. import org.log5f.appenders.LocalConnectionAppender;
  12. import org.log5f.appenders.TraceAppender;
  13. import org.log5f.appenders.XMLSocketAppender;
  14. import org.log5f.appenders.XPanelAppender;
  15. import org.log5f.core.config.Config;
  16. import org.log5f.filters.DenyAllFilter;
  17. import org.log5f.filters.LevelRangeFilter;
  18. import org.log5f.filters.StringMatchFilter;
  19. import org.log5f.layouts.Log4JLayout;
  20. import org.log5f.layouts.PatternLayout;
  21. import org.log5f.layouts.SimpleLayout;
  22. import org.log5f.layouts.ThunderBoltLayout;
  23. use namespace log5f_internal;
  24. /**
  25. * The <code>LoggerManager</code> class contains public methods for
  26. * configuration Log5F:
  27. *
  28. * <pre>
  29. * // allows to configure from remote file
  30. * LoggerManager.configure("http://example.com/log5f/config.xml");
  31. *
  32. * // and from remote file
  33. * LoggerManager.configure(<config>...</config>);
  34. *
  35. * // checking if tracing errors is enabled
  36. * trace(LoggerManager.traceErrors);
  37. * </pre>
  38. *
  39. * It contains two methods for retrieving loggers, but these methods are
  40. * deprecated, and replaced by static methods from the <code>Logger</code>
  41. * class.
  42. *
  43. * <pre>
  44. *
  45. * package my
  46. * {
  47. * import org.log5f.Logger;
  48. * import org.log5f.ILogger;
  49. *
  50. * public class MyClass
  51. * {
  52. * private static const logger:ILogger = Logger.getLogger(MyClass);
  53. * ...
  54. * }
  55. * }
  56. *
  57. * </pre>
  58. *
  59. * <p>
  60. * Under the hood the <code>LoggerManager</code> creates hierarchical
  61. * structure of the <code>Logger</code> classes and store it.
  62. * </p>
  63. *
  64. * @see org.log5f.Logger
  65. * @see org.log5f.ILogger
  66. */
  67. public class LoggerManager
  68. {
  69. //----------------------------------------------------------------------
  70. //
  71. // Class constants
  72. //
  73. //----------------------------------------------------------------------
  74. /**
  75. * The name of root logger.
  76. */
  77. public static const ROOT_LOGGER_NAME:String = "root";
  78. //----------------------------------------------------------------------
  79. //
  80. // Class variables
  81. //
  82. //----------------------------------------------------------------------
  83. /**
  84. * Stores all loggers which creating in the process of working of
  85. * application.
  86. */
  87. private static var loggers:Object;
  88. //----------------------------------------------------------------------
  89. //
  90. // Class variables
  91. //
  92. //----------------------------------------------------------------------
  93. //-----------------------------------
  94. // ready
  95. //-----------------------------------
  96. /**
  97. * Storage for enabled property.
  98. */
  99. private static var _enabled:Boolean = true;
  100. /**
  101. * Indicates if Log5F is <i>enabled</i>.
  102. *
  103. * <p>If this property is <code>true</code> this means that least one
  104. * configuration process was successfull.</p>
  105. *
  106. * <p><b>Note</b>: If this property is <code>false</code>, all logs
  107. * will be ignored.</p>
  108. *
  109. */
  110. log5f_internal static function get enabled():Boolean
  111. {
  112. return _enabled;
  113. }
  114. /**
  115. * @private
  116. */
  117. log5f_internal static function set enabled(value:Boolean):void
  118. {
  119. _enabled = value;
  120. }
  121. //----------------------------------------------------------------------
  122. //
  123. // Class methods
  124. //
  125. //----------------------------------------------------------------------
  126. //-----------------------------------
  127. // Class methods: Configuration
  128. //-----------------------------------
  129. /**
  130. * @copy Config.configure()
  131. */
  132. public static function configure(source:Object):void
  133. {
  134. Config.configure(source);
  135. }
  136. /**
  137. * @copy Config.traceErrors
  138. */
  139. public static function get traceErrors():Boolean
  140. {
  141. return Config.traceErrors;
  142. }
  143. //-----------------------------------
  144. // Class methods: Retrieving
  145. //-----------------------------------
  146. /**
  147. * Retrives root logger.
  148. *
  149. * @return Root logger.
  150. */
  151. public static function getRootLogger():Logger
  152. {
  153. return getLogger(ROOT_LOGGER_NAME);
  154. }
  155. /**
  156. * Retrieves the appropriate <code>Logger</code> instance.
  157. *
  158. * @param key Specifies category of creating logger. This parameter can
  159. * be a string or class or an instance.
  160. *
  161. * @return Appropriate logger.
  162. */
  163. public static function getLogger(key:*):Logger
  164. {
  165. var name:String;
  166. if (key is String)
  167. {
  168. name = key as String;
  169. }
  170. else if (key is Class || key is Object)
  171. {
  172. var info:XML = describeType(key);
  173. name = info.@name[0].toString().replace("::", ".");
  174. }
  175. return getLoggerByName(name);
  176. }
  177. /**
  178. * Retrives or creates appropriate <code>Logger</code> instance.
  179. *
  180. * @param name The name of category of Logger.
  181. *
  182. * @return Appropriate logger.
  183. *
  184. * @see org.log5f.Logger#category
  185. */
  186. protected static function getLoggerByName(name:String):Logger
  187. {
  188. if (loggers == null)
  189. loggers = {};
  190. name = (name == null || name == "") ? ROOT_LOGGER_NAME : name;
  191. if (!loggers[name])
  192. {
  193. loggers[name] = new Logger(name);
  194. insertInHierarchy(loggers[name]);
  195. }
  196. return loggers[name];
  197. }
  198. /**
  199. * This method does nothing, it used for force compile specified class.
  200. *
  201. * You can create custom appenders, filters, formatters and layouts, but
  202. * they are not using in project, usually. Therefore flash compiler
  203. * doesn't compile they into result swf. For resolving this issue you
  204. * can use this method.
  205. *
  206. * <pre>
  207. * LoggerManager.forceCompile(MyAppender);
  208. * </pre>
  209. *
  210. * @param someClass A some class that is not used explicitly in project,
  211. * but can be instantiated during run time.
  212. */
  213. public static function forceCompile(someClass:Class):void
  214. {
  215. // stub
  216. }
  217. /**
  218. * Inserts specified logger into hierarchy.
  219. *
  220. * @param logger The logger that need add to hierarchy.
  221. */
  222. private static function insertInHierarchy(logger:Logger):void
  223. {
  224. // find children
  225. for each (var child:Logger in loggers)
  226. {
  227. if (child == logger)
  228. continue;
  229. if (child.name.indexOf(logger.name) != 0)
  230. continue;
  231. if (child.parent == LoggerManager.getRootLogger() ||
  232. child.parent.name.length > logger.name.length)
  233. {
  234. child.parent = logger;
  235. }
  236. }
  237. // find parent
  238. var packages:Array = logger.name.split(".");
  239. packages.pop();
  240. while (packages.length > 0)
  241. {
  242. var name:String = packages.join(".");
  243. if (loggers[name])
  244. {
  245. logger.parent = loggers[name];
  246. break;
  247. }
  248. packages.pop();
  249. }
  250. if (!logger.parent && logger != LoggerManager.getRootLogger())
  251. logger.parent = LoggerManager.getRootLogger();
  252. }
  253. //----------------------------------------------------------------------
  254. //
  255. // Constructor
  256. //
  257. //----------------------------------------------------------------------
  258. /**
  259. * Constructor.
  260. */
  261. public function LoggerManager()
  262. {
  263. super();
  264. LoggerManager.forceCompile(DenyAllFilter);
  265. LoggerManager.forceCompile(LevelRangeFilter);
  266. LoggerManager.forceCompile(StringMatchFilter);
  267. LoggerManager.forceCompile(Log4JLayout);
  268. LoggerManager.forceCompile(SimpleLayout);
  269. LoggerManager.forceCompile(PatternLayout);
  270. LoggerManager.forceCompile(ThunderBoltLayout);
  271. LoggerManager.forceCompile(TraceAppender);
  272. LoggerManager.forceCompile(XPanelAppender);
  273. LoggerManager.forceCompile(BrowserAppender);
  274. LoggerManager.forceCompile(FirebugAppender);
  275. LoggerManager.forceCompile(XMLSocketAppender);
  276. LoggerManager.forceCompile(ArthropodAppender);
  277. LoggerManager.forceCompile(LocalConnectionAppender);
  278. }
  279. }
  280. }