PageRenderTime 175ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/SyracavaPHP/syracava/ext/log4php/LoggerHierarchy.php

http://syracava.googlecode.com/
PHP | 388 lines | 169 code | 37 blank | 182 comment | 18 complexity | 6e877f9c9fba0dd96d633b639aad8d64 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. */
  18. /**
  19. * @ignore
  20. */
  21. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
  22. /**
  23. */
  24. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  25. require_once(LOG4PHP_DIR . '/LoggerLevel.php');
  26. require_once(LOG4PHP_DIR . '/LoggerRoot.php');
  27. require_once(LOG4PHP_DIR . '/or/LoggerRendererMap.php');
  28. require_once(LOG4PHP_DIR . '/LoggerDefaultCategoryFactory.php');
  29. /**
  30. * This class is specialized in retrieving loggers by name and also maintaining
  31. * the logger hierarchy.
  32. *
  33. * <p>The casual user does not have to deal with this class directly.</p>
  34. *
  35. * <p>The structure of the logger hierarchy is maintained by the
  36. * getLogger method. The hierarchy is such that children link
  37. * to their parent but parents do not have any pointers to their
  38. * children. Moreover, loggers can be instantiated in any order, in
  39. * particular descendant before ancestor.</p>
  40. *
  41. * <p>In case a descendant is created before a particular ancestor,
  42. * then it creates a provision node for the ancestor and adds itself
  43. * to the provision node. Other descendants of the same ancestor add
  44. * themselves to the previously created provision node.</p>
  45. *
  46. * @author VxR <vxr@vxr.it>
  47. * @version $Revision: 125 $
  48. * @package log4php
  49. */
  50. class LoggerHierarchy {
  51. /**
  52. * @var object currently unused
  53. */
  54. var $defaultFactory;
  55. /**
  56. * @var boolean activate internal logging
  57. * @see LoggerLog
  58. */
  59. var $debug = false;
  60. /**
  61. * @var array hierarchy tree. saves here all loggers
  62. */
  63. var $ht = array();
  64. /**
  65. * @var LoggerRoot
  66. */
  67. var $root = null;
  68. /**
  69. * @var LoggerRendererMap
  70. */
  71. var $rendererMap;
  72. /**
  73. * @var LoggerLevel main level threshold
  74. */
  75. var $threshold;
  76. /**
  77. * @var boolean currently unused
  78. */
  79. var $emittedNoAppenderWarning = false;
  80. /**
  81. * @var boolean currently unused
  82. */
  83. var $emittedNoResourceBundleWarning = false;
  84. /* --------------------------------------------------------------------------*/
  85. /* --------------------------------------------------------------------------*/
  86. /* --------------------------------------------------------------------------*/
  87. function &singleton()
  88. {
  89. static $instance;
  90. if (!isset($instance))
  91. $instance = new LoggerHierarchy(new LoggerRoot());
  92. return $instance;
  93. }
  94. /**
  95. * Create a new logger hierarchy.
  96. * @param object $root the root logger
  97. */
  98. function LoggerHierarchy($root)
  99. {
  100. $this->root =& $root;
  101. // Enable all level levels by default.
  102. $this->setThreshold(LoggerLevel::getLevelAll());
  103. $this->root->setHierarchy($this);
  104. $this->rendererMap = new LoggerRendererMap();
  105. $this->defaultFactory = new LoggerDefaultCategoryFactory();
  106. }
  107. /**
  108. * Add a HierarchyEventListener event to the repository.
  109. * Not Yet Impl.
  110. */
  111. function addHierarchyEventListener($listener)
  112. {
  113. return;
  114. }
  115. /**
  116. * Add an object renderer for a specific class.
  117. * Not Yet Impl.
  118. */
  119. function addRenderer($classToRender, $or)
  120. {
  121. $this->rendererMap->put($classToRender, $or);
  122. }
  123. /**
  124. * This call will clear all logger definitions from the internal hashtable.
  125. */
  126. function clear()
  127. {
  128. $this->ht = array();
  129. }
  130. function emitNoAppenderWarning($cat)
  131. {
  132. return;
  133. }
  134. /**
  135. * Check if the named logger exists in the hierarchy.
  136. * @param string $name
  137. * @return boolean
  138. */
  139. function exists($name)
  140. {
  141. return in_array($name, array_keys($this->ht));
  142. }
  143. function fireAddAppenderEvent($logger, $appender)
  144. {
  145. return;
  146. }
  147. /**
  148. * @deprecated Please use {@link getCurrentLoggers()} instead.
  149. */
  150. function &getCurrentCategories()
  151. {
  152. return $this->getCurrentLoggers();
  153. }
  154. /**
  155. * Returns all the currently defined categories in this hierarchy as an array.
  156. * @return array
  157. */
  158. function &getCurrentLoggers()
  159. {
  160. $loggers = array();
  161. $loggerNames = array_keys($this->ht);
  162. $enumLoggers = sizeof($loggerNames);
  163. for ($i = 0; $i < $enumLoggers; $i++) {
  164. $loggerName = $loggerNames[$i];
  165. $loggers[] =& $this->ht[$loggerName];
  166. }
  167. return $loggers;
  168. }
  169. /**
  170. * Return a new logger instance named as the first parameter using the default factory.
  171. *
  172. * @param string $name logger name
  173. * @param LoggerFactory $factory a {@link LoggerFactory} instance or null
  174. * @return Logger
  175. */
  176. function &getLogger($name, $factory = null)
  177. {
  178. if ($factory === null) {
  179. return $this->getLoggerByFactory($name, $this->defaultFactory);
  180. } else {
  181. return $this->getLoggerByFactory($name, $factory);
  182. }
  183. }
  184. /**
  185. * Return a new logger instance named as the first parameter using the default factory.
  186. *
  187. * @param string $name logger name
  188. * @return Logger
  189. * @todo merge with {@link getLogger()}
  190. */
  191. function &getLoggerByFactory($name, $factory)
  192. {
  193. if (!isset($this->ht[$name])) {
  194. LoggerLog::debug("LoggerHierarchy::getLoggerByFactory():name=[$name]:factory=[".get_class($factory)."] creating a new logger...");
  195. $this->ht[$name] = $factory->makeNewLoggerInstance($name);
  196. $this->ht[$name]->setHierarchy($this);
  197. $nodes = explode('.', $name);
  198. $firstNode = array_shift($nodes);
  199. if ( $firstNode != $name and isset($this->ht[$firstNode])) {
  200. LoggerLog::debug("LoggerHierarchy::getLogger($name) parent is now [$firstNode]");
  201. $this->ht[$name]->parent =& $this->ht[$firstNode];
  202. } else {
  203. LoggerLog::debug("LoggerHierarchy::getLogger($name) parent is now [root]");
  204. $this->ht[$name]->parent =& $this->root;
  205. }
  206. if (sizeof($nodes) > 0) {
  207. // find parent node
  208. foreach ($nodes as $node) {
  209. $parentNode = "$firstNode.$node";
  210. if (isset($this->ht[$parentNode]) and $parentNode != $name) {
  211. LoggerLog::debug("LoggerHierarchy::getLogger($name) parent is now [$parentNode]");
  212. $this->ht[$name]->parent =& $this->ht[$parentNode];
  213. }
  214. $firstNode .= ".$node";
  215. }
  216. }
  217. // update children
  218. /*
  219. $children = array();
  220. foreach (array_keys($this->ht) as $nodeName) {
  221. if ($nodeName != $name and substr($nodeName, 0, strlen($name)) == $name) {
  222. $children[] = $nodeName;
  223. }
  224. }
  225. */
  226. }
  227. return $this->ht[$name];
  228. }
  229. /**
  230. * @return LoggerRendererMap Get the renderer map for this hierarchy.
  231. */
  232. function &getRendererMap()
  233. {
  234. return $this->rendererMap;
  235. }
  236. /**
  237. * @return LoggerRoot Get the root of this hierarchy.
  238. */
  239. function &getRootLogger()
  240. {
  241. if (!isset($this->root) or $this->root == null)
  242. $this->root = new LoggerRoot();
  243. return $this->root;
  244. }
  245. /**
  246. * @return LoggerLevel Returns the threshold Level.
  247. */
  248. function getThreshold()
  249. {
  250. return $this->threshold;
  251. }
  252. /**
  253. * This method will return true if this repository is disabled
  254. * for level object passed as parameter and false otherwise.
  255. * @return boolean
  256. */
  257. function isDisabled($level)
  258. {
  259. return ($this->threshold->level > $level->level);
  260. }
  261. /**
  262. * @deprecated Deprecated with no replacement.
  263. */
  264. function overrideAsNeeded($override)
  265. {
  266. return;
  267. }
  268. /**
  269. * Reset all values contained in this hierarchy instance to their
  270. * default.
  271. *
  272. * This removes all appenders from all categories, sets
  273. * the level of all non-root categories to <i>null</i>,
  274. * sets their additivity flag to <i>true</i> and sets the level
  275. * of the root logger to {@link LOGGER_LEVEL_DEBUG}. Moreover,
  276. * message disabling is set its default "off" value.
  277. *
  278. * <p>Existing categories are not removed. They are just reset.
  279. *
  280. * <p>This method should be used sparingly and with care as it will
  281. * block all logging until it is completed.</p>
  282. */
  283. function resetConfiguration()
  284. {
  285. $root =& $this->getRootLogger();
  286. $root->setLevel(LoggerLevel::getLevelDebug());
  287. $this->setThreshold(LoggerLevel::getLevelAll());
  288. $this->shutDown();
  289. $loggers =& $this->getCurrentLoggers();
  290. $enumLoggers = sizeof($loggers);
  291. for ($i = 0; $i < $enumLoggers; $i++) {
  292. $loggers[$i]->setLevel(null);
  293. $loggers[$i]->setAdditivity(true);
  294. $loggers[$i]->setResourceBundle(null);
  295. }
  296. $this->rendererMap->clear();
  297. }
  298. /**
  299. * @deprecated Deprecated with no replacement.
  300. */
  301. function setDisableOverride($override)
  302. {
  303. return;
  304. }
  305. /**
  306. * Used by subclasses to add a renderer to the hierarchy passed as parameter.
  307. * @param string $renderedClass a LoggerRenderer class name
  308. * @param LoggerRenderer $renderer
  309. *
  310. */
  311. function setRenderer($renderedClass, $renderer)
  312. {
  313. $this->rendererMap->put($renderedClass, $renderer);
  314. }
  315. /**
  316. * set a new threshold level
  317. *
  318. * @param LoggerLevel $l
  319. */
  320. function setThreshold($l)
  321. {
  322. if ($l !== null)
  323. $this->threshold = $l;
  324. }
  325. /**
  326. * Shutting down a hierarchy will <i>safely</i> close and remove
  327. * all appenders in all categories including the root logger.
  328. *
  329. * <p>Some appenders such as {@link LoggerSocketAppender}
  330. * need to be closed before the
  331. * application exists. Otherwise, pending logging events might be
  332. * lost.
  333. *
  334. * <p>The shutdown method is careful to close nested
  335. * appenders before closing regular appenders. This is allows
  336. * configurations where a regular appender is attached to a logger
  337. * and again to a nested appender.
  338. */
  339. function shutdown()
  340. {
  341. $this->root->removeAllAppenders();
  342. $cats =& $this->getCurrentLoggers();
  343. $enumCats = sizeof($cats);
  344. if ($enumCats > 0) {
  345. for ($i = 0; $i < $enumCats; $i++) {
  346. $cats[$i]->removeAllAppenders();
  347. }
  348. }
  349. }
  350. }
  351. ?>