LoggerCategory.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 . '/LoggerLevel.php');
  25. require_once(LOG4PHP_DIR . '/spi/LoggerLoggingEvent.php');
  26. /**
  27. * This class has been deprecated and replaced by the Logger subclass.
  28. *
  29. * @author VxR <vxr@vxr.it>
  30. * @version $Revision: 1.1 $
  31. * @package log4php
  32. * @see Logger
  33. */
  34. class LoggerCategory {
  35. /**
  36. * Additivity is set to true by default, that is children inherit the
  37. * appenders of their ancestors by default.
  38. * @var boolean
  39. */
  40. var $additive = true;
  41. /**
  42. * @var string fully qualified class name
  43. */
  44. var $fqcn = 'LoggerCategory';
  45. /**
  46. * @var LoggerLevel The assigned level of this category.
  47. */
  48. var $level = null;
  49. /**
  50. * @var string name of this category.
  51. */
  52. var $name = '';
  53. /**
  54. * @var Logger The parent of this category.
  55. */
  56. var $parent = null;
  57. /**
  58. * @var LoggerHierarchy the object repository
  59. */
  60. var $repository = null;
  61. /**
  62. * @var array collection of appenders
  63. * @see LoggerAppender
  64. */
  65. var $aai = array();
  66. /* --------------------------------------------------------------------------*/
  67. /* --------------------------------------------------------------------------*/
  68. /* --------------------------------------------------------------------------*/
  69. /**
  70. * Constructor.
  71. *
  72. * @param string $name Category name
  73. */
  74. function LoggerCategory($name)
  75. {
  76. $this->name = $name;
  77. }
  78. /**
  79. * Add a new Appender to the list of appenders of this Category instance.
  80. *
  81. * @param LoggerAppender $newAppender
  82. */
  83. function addAppender(&$newAppender)
  84. {
  85. $appenderName = $newAppender->getName();
  86. $this->aai[$appenderName] =& $newAppender;
  87. }
  88. /**
  89. * If assertion parameter is false, then logs msg as an error statement.
  90. *
  91. * @param bool $assertion
  92. * @param string $msg message to log
  93. */
  94. function assertLog($assertion = true, $msg = '')
  95. {
  96. if ($assertion == false) {
  97. $this->error($msg);
  98. }
  99. }
  100. /**
  101. * Call the appenders in the hierarchy starting at this.
  102. *
  103. * @param LoggerLoggingEvent $event
  104. */
  105. function callAppenders($event)
  106. {
  107. if (sizeof($this->aai) > 0) {
  108. foreach (array_keys($this->aai) as $appenderName) {
  109. $this->aai[$appenderName]->doAppend($event);
  110. }
  111. }
  112. if ($this->parent != null and $this->getAdditivity()) {
  113. $this->parent->callAppenders($event);
  114. }
  115. }
  116. /**
  117. * Log a message object with the DEBUG level including the caller.
  118. *
  119. * @param mixed $message message
  120. * @param mixed $caller caller object or caller string id
  121. */
  122. function debug($message, $caller = null)
  123. {
  124. $debugLevel = LoggerLevel::getLevelDebug();
  125. if ($this->repository->isDisabled($debugLevel)) {
  126. return;
  127. }
  128. if ($debugLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
  129. $this->forcedLog($this->fqcn, $caller, $debugLevel, $message);
  130. }
  131. }
  132. /**
  133. * Log a message object with the ERROR level including the caller.
  134. *
  135. * @param mixed $message message
  136. * @param mixed $caller caller object or caller string id
  137. */
  138. function error($message, $caller = null)
  139. {
  140. $errorLevel = LoggerLevel::getLevelError();
  141. if ($this->repository->isDisabled($errorLevel)) {
  142. return;
  143. }
  144. if ($errorLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
  145. $this->forcedLog($this->fqcn, $caller, $errorLevel, $message);
  146. }
  147. }
  148. /**
  149. * Deprecated. Please use LoggerManager::exists() instead.
  150. *
  151. * @param string $name
  152. * @see LoggerManager::exists()
  153. * @deprecated
  154. */
  155. function exists($name)
  156. {
  157. return LoggerManager::exists($name);
  158. }
  159. /**
  160. * Log a message object with the FATAL level including the caller.
  161. *
  162. * @param mixed $message message
  163. * @param mixed $caller caller object or caller string id
  164. */
  165. function fatal($message, $caller = null)
  166. {
  167. $fatalLevel = LoggerLevel::getLevelFatal();
  168. if ($this->repository->isDisabled($fatalLevel)) {
  169. return;
  170. }
  171. if ($fatalLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
  172. $this->forcedLog($this->fqcn, $caller, $fatalLevel, $message);
  173. }
  174. }
  175. /**
  176. * This method creates a new logging event and logs the event without further checks.
  177. *
  178. * It should not be called directly. Use {@link info()}, {@link debug()}, {@link warn()},
  179. * {@link error()} and {@link fatal()} wrappers.
  180. *
  181. * @param string $fqcn Fully Qualified Class Name of the Logger
  182. * @param mixed $caller caller object or caller string id
  183. * @param LoggerLevel $level log level
  184. * @param mixed $message message
  185. * @see LoggerLoggingEvent
  186. */
  187. function forcedLog($fqcn, $caller, $level, $message)
  188. {
  189. // $fqcn = is_object($caller) ? get_class($caller) : (string)$caller;
  190. $this->callAppenders(new LoggerLoggingEvent($fqcn, $this, $level, $message));
  191. }
  192. /**
  193. * Get the additivity flag for this Category instance.
  194. * @return boolean
  195. */
  196. function getAdditivity()
  197. {
  198. return $this->additive;
  199. }
  200. /**
  201. * Get the appenders contained in this category as an array.
  202. * @return array collection of appenders
  203. */
  204. function &getAllAppenders()
  205. {
  206. $appenders = array();
  207. $appenderNames = array_keys($this->aai);
  208. $enumAppenders = sizeof($appenderNames);
  209. for ($i = 0; $i < $enumAppenders; $i++) {
  210. $appenderName = $appenderNames[$i];
  211. $appenders[] =& $this->aai[$appenderName];
  212. }
  213. return $appenders;
  214. }
  215. /**
  216. * Look for the appender named as name.
  217. * @return LoggerAppender
  218. */
  219. function &getAppender($name)
  220. {
  221. return $this->aai[$name];
  222. }
  223. /**
  224. * Please use the {@link getEffectiveLevel()} method instead.
  225. * @deprecated
  226. */
  227. function getChainedPriority()
  228. {
  229. return $this->getEffectiveLevel();
  230. }
  231. /**
  232. * Please use {@link LoggerManager::getCurrentLoggers()} instead.
  233. * @deprecated
  234. */
  235. function getCurrentCategories()
  236. {
  237. return LoggerManager::getCurrentLoggers();
  238. }
  239. /**
  240. * Please use {@link LoggerManager::getLoggerRepository()} instead.
  241. * @deprecated
  242. */
  243. function &getDefaultHierarchy()
  244. {
  245. return LoggerManager::getLoggerRepository();
  246. }
  247. /**
  248. * @deprecated Use {@link getLoggerRepository()}
  249. * @return LoggerHierarchy
  250. */
  251. function &getHierarchy()
  252. {
  253. return $this->getLoggerRepository();
  254. }
  255. /**
  256. * Starting from this category, search the category hierarchy for a non-null level and return it.
  257. * @see LoggerLevel
  258. * @return LoggerLevel or null
  259. */
  260. function getEffectiveLevel()
  261. {
  262. for($c = $this; $c != null; $c = $c->parent) {
  263. if($c->level !== null)
  264. return $c->level;
  265. }
  266. return null;
  267. }
  268. /**
  269. * Retrieve a category with named as the name parameter.
  270. * @return Logger
  271. */
  272. function &getInstance($name)
  273. {
  274. return LoggerManager::getLogger($name);
  275. }
  276. /**
  277. * Returns the assigned Level, if any, for this Category.
  278. * @return LoggerLevel or null
  279. */
  280. function getLevel()
  281. {
  282. return $this->level;
  283. }
  284. /**
  285. * Return the the repository where this Category is attached.
  286. * @return LoggerHierarchy
  287. */
  288. function &getLoggerRepository()
  289. {
  290. return $this->repository;
  291. }
  292. /**
  293. * Return the category name.
  294. * @return string
  295. */
  296. function getName()
  297. {
  298. return $this->name;
  299. }
  300. /**
  301. * Returns the parent of this category.
  302. * @return Logger
  303. */
  304. function &getParent()
  305. {
  306. return $this->parent;
  307. }
  308. /**
  309. * Please use getLevel() instead.
  310. * @deprecated
  311. */
  312. function getPriority()
  313. {
  314. return $this->getLevel();
  315. }
  316. /**
  317. * Return the inherited ResourceBundle for this category.
  318. */
  319. function getResourceBundle()
  320. {
  321. return;
  322. }
  323. /**
  324. * Returns the string resource coresponding to key in this category's inherited resource bundle.
  325. */
  326. function getResourceBundleString($key)
  327. {
  328. return;
  329. }
  330. /**
  331. * Return the root of the default category hierrachy.
  332. * @return LoggerRoot
  333. */
  334. function &getRoot()
  335. {
  336. return LoggerManager::getRootLogger();
  337. }
  338. /**
  339. * Log a message object with the INFO Level.
  340. *
  341. * @param mixed $message message
  342. * @param mixed $caller caller object or caller string id
  343. */
  344. function info($message, $caller = null)
  345. {
  346. $infoLevel = LoggerLevel::getLevelInfo();
  347. if ($this->repository->isDisabled($infoLevel)) {
  348. return;
  349. }
  350. if ($infoLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
  351. $this->forcedLog($this->fqcn, $caller, $infoLevel, $message);
  352. }
  353. }
  354. /**
  355. * Is the appender passed as parameter attached to this category?
  356. *
  357. * @param LoggerAppender $appender
  358. */
  359. function isAttached($appender)
  360. {
  361. return in_array($appender->getName(), array_keys($this->aai));
  362. }
  363. /**
  364. * Check whether this category is enabled for the DEBUG Level.
  365. * @return boolean
  366. */
  367. function isDebugEnabled()
  368. {
  369. $debugLevel = LoggerLevel::getLevelDebug();
  370. if ($this->repository->isDisabled($debugLevel)) {
  371. return false;
  372. }
  373. return ($debugLevel->isGreaterOrEqual($this->getEffectiveLevel()));
  374. }
  375. /**
  376. * Check whether this category is enabled for a given Level passed as parameter.
  377. *
  378. * @param LoggerLevel level
  379. * @return boolean
  380. */
  381. function isEnabledFor($level)
  382. {
  383. if ($this->repository->isDisabled($level)) {
  384. return false;
  385. }
  386. return (bool)($level->isGreaterOrEqual($this->getEffectiveLevel()));
  387. }
  388. /**
  389. * Check whether this category is enabled for the info Level.
  390. * @return boolean
  391. * @see LoggerLevel
  392. */
  393. function isInfoEnabled()
  394. {
  395. $infoLevel = LoggerLevel::getLevelInfo();
  396. if ($this->repository->isDisabled($infoLevel)) {
  397. return false;
  398. }
  399. return ($infoLevel->isGreaterOrEqual($this->getEffectiveLevel()));
  400. }
  401. /**
  402. * Log a localized and parameterized message.
  403. */
  404. function l7dlog($priority, $key, $params, $t)
  405. {
  406. return;
  407. }
  408. /**
  409. * This generic form is intended to be used by wrappers.
  410. *
  411. * @param LoggerLevel $priority a valid level
  412. * @param mixed $message message
  413. * @param mixed $caller caller object or caller string id
  414. */
  415. function log($priority, $message, $caller = null)
  416. {
  417. if ($this->repository->isDisabled($priority)) {
  418. return;
  419. }
  420. if ($priority->isGreaterOrEqual($this->getEffectiveLevel())) {
  421. $this->forcedLog($this->fqcn, $caller, $priority, $message);
  422. }
  423. }
  424. /**
  425. * Remove all previously added appenders from this Category instance.
  426. */
  427. function removeAllAppenders()
  428. {
  429. $appenderNames = array_keys($this->aai);
  430. $enumAppenders = sizeof($appenderNames);
  431. for ($i = 0; $i < $enumAppenders; $i++) {
  432. $this->removeAppender($appenderNames[$i]);
  433. }
  434. }
  435. /**
  436. * Remove the appender passed as parameter form the list of appenders.
  437. *
  438. * @param mixed $appender can be an appender name or a {@link LoggerAppender} object
  439. */
  440. function removeAppender($appender)
  441. {
  442. if (is_a($appender, 'loggerappender')) {
  443. $appender->close();
  444. unset($this->aai[$appender->getName()]);
  445. } elseif (is_string($appender) and isset($this->aai[$appender])) {
  446. $this->aai[$appender]->close();
  447. unset($this->aai[$appender]);
  448. }
  449. }
  450. /**
  451. * Set the additivity flag for this Category instance.
  452. *
  453. * @param boolean $additive
  454. */
  455. function setAdditivity($additive)
  456. {
  457. $this->additive = (bool)$additive;
  458. }
  459. /**
  460. * @deprecated Please use {@link setLevel()} instead.
  461. * @see setLevel()
  462. */
  463. function setPriority($priority)
  464. {
  465. $this->setLevel($priority);
  466. }
  467. /**
  468. * Only the Hiearchy class can set the hiearchy of a
  469. * category.
  470. *
  471. * @param LoggerHierarchy &$repository
  472. */
  473. function setHierarchy(&$repository)
  474. {
  475. $this->repository =& $repository;
  476. }
  477. /**
  478. * Set the level of this Category.
  479. *
  480. * @param LoggerLevel $level a level string or a level costant
  481. */
  482. function setLevel($level)
  483. {
  484. $this->level = $level;
  485. }
  486. /**
  487. * Set the resource bundle to be used with localized logging methods
  488. */
  489. function setResourceBundle($bundle)
  490. {
  491. return;
  492. }
  493. /**
  494. * @deprecated use {@link LoggerManager::shutdown()} instead.
  495. * @see LoggerManager::shutdown()
  496. */
  497. function shutdown()
  498. {
  499. LoggerManager::shutdown();
  500. }
  501. /**
  502. * Log a message with the WARN level.
  503. *
  504. * @param mixed $message message
  505. * @param mixed $caller caller object or caller string id
  506. */
  507. function warn($message, $caller = null)
  508. {
  509. $warnLevel = LoggerLevel::getLevelWarn();
  510. if ($this->repository->isDisabled($warnLevel)) {
  511. return;
  512. }
  513. if ($warnLevel->isGreaterOrEqual($this->getEffectiveLevel())) {
  514. $this->forcedLog($this->fqcn, $caller, $warnLevel, $message);
  515. }
  516. }
  517. }
  518. ?>