PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/cubi/openbiz/others/Zend/Log.php

http://openbiz-cubi.googlecode.com/
PHP | 222 lines | 86 code | 22 blank | 114 comment | 10 complexity | 89dc5b7a86709f8a24bdd863c0daa0b9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Log
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Log.php 18951 2009-11-12 16:26:19Z alexander $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Log
  24. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. * @version $Id: Log.php 18951 2009-11-12 16:26:19Z alexander $
  27. */
  28. class Zend_Log
  29. {
  30. const EMERG = 0; // Emergency: system is unusable
  31. const ALERT = 1; // Alert: action must be taken immediately
  32. const CRIT = 2; // Critical: critical conditions
  33. const ERR = 3; // Error: error conditions
  34. const WARN = 4; // Warning: warning conditions
  35. const NOTICE = 5; // Notice: normal but significant condition
  36. const INFO = 6; // Informational: informational messages
  37. const DEBUG = 7; // Debug: debug messages
  38. /**
  39. * @var array of priorities where the keys are the
  40. * priority numbers and the values are the priority names
  41. */
  42. protected $_priorities = array();
  43. /**
  44. * @var array of Zend_Log_Writer_Abstract
  45. */
  46. protected $_writers = array();
  47. /**
  48. * @var array of Zend_Log_Filter_Interface
  49. */
  50. protected $_filters = array();
  51. /**
  52. * @var array of extra log event
  53. */
  54. protected $_extras = array();
  55. /**
  56. * Class constructor. Create a new logger
  57. *
  58. * @param Zend_Log_Writer_Abstract|null $writer default writer
  59. */
  60. public function __construct(Zend_Log_Writer_Abstract $writer = null)
  61. {
  62. $r = new ReflectionClass($this);
  63. $this->_priorities = array_flip($r->getConstants());
  64. if ($writer !== null) {
  65. $this->addWriter($writer);
  66. }
  67. }
  68. /**
  69. * Class destructor. Shutdown log writers
  70. *
  71. * @return void
  72. */
  73. public function __destruct()
  74. {
  75. foreach($this->_writers as $writer) {
  76. $writer->shutdown();
  77. }
  78. }
  79. /**
  80. * Undefined method handler allows a shortcut:
  81. * $log->priorityName('message')
  82. * instead of
  83. * $log->log('message', Zend_Log::PRIORITY_NAME)
  84. *
  85. * @param string $method priority name
  86. * @param string $params message to log
  87. * @return void
  88. * @throws Zend_Log_Exception
  89. */
  90. public function __call($method, $params)
  91. {
  92. $priority = strtoupper($method);
  93. if (($priority = array_search($priority, $this->_priorities)) !== false) {
  94. $this->log(array_shift($params), $priority);
  95. } else {
  96. /** @see Zend_Log_Exception */
  97. // require_once 'Zend/Log/Exception.php';
  98. throw new Zend_Log_Exception('Bad log priority');
  99. }
  100. }
  101. /**
  102. * Log a message at a priority
  103. *
  104. * @param string $message Message to log
  105. * @param integer $priority Priority of message
  106. * @return void
  107. * @throws Zend_Log_Exception
  108. */
  109. public function log($message, $priority)
  110. {
  111. // sanity checks
  112. if (empty($this->_writers)) {
  113. /** @see Zend_Log_Exception */
  114. // require_once 'Zend/Log/Exception.php';
  115. throw new Zend_Log_Exception('No writers were added');
  116. }
  117. if (! isset($this->_priorities[$priority])) {
  118. /** @see Zend_Log_Exception */
  119. // require_once 'Zend/Log/Exception.php';
  120. throw new Zend_Log_Exception('Bad log priority');
  121. }
  122. // pack into event required by filters and writers
  123. $event = array_merge(array('timestamp' => date('c'),
  124. 'message' => $message,
  125. 'priority' => $priority,
  126. 'priorityName' => $this->_priorities[$priority]),
  127. $this->_extras);
  128. // abort if rejected by the global filters
  129. foreach ($this->_filters as $filter) {
  130. if (! $filter->accept($event)) {
  131. return;
  132. }
  133. }
  134. // send to each writer
  135. foreach ($this->_writers as $writer) {
  136. $writer->write($event);
  137. }
  138. }
  139. /**
  140. * Add a custom priority
  141. *
  142. * @param string $name Name of priority
  143. * @param integer $priority Numeric priority
  144. * @throws Zend_Log_InvalidArgumentException
  145. */
  146. public function addPriority($name, $priority)
  147. {
  148. // Priority names must be uppercase for predictability.
  149. $name = strtoupper($name);
  150. if (isset($this->_priorities[$priority])
  151. || array_search($name, $this->_priorities)) {
  152. /** @see Zend_Log_Exception */
  153. // require_once 'Zend/Log/Exception.php';
  154. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  155. }
  156. $this->_priorities[$priority] = $name;
  157. }
  158. /**
  159. * Add a filter that will be applied before all log writers.
  160. * Before a message will be received by any of the writers, it
  161. * must be accepted by all filters added with this method.
  162. *
  163. * @param int|Zend_Log_Filter_Interface $filter
  164. * @return void
  165. */
  166. public function addFilter($filter)
  167. {
  168. if (is_integer($filter)) {
  169. /** @see Zend_Log_Filter_Priority */
  170. // require_once 'Zend/Log/Filter/Priority.php';
  171. $filter = new Zend_Log_Filter_Priority($filter);
  172. } elseif(!is_object($filter) || ! $filter instanceof Zend_Log_Filter_Interface) {
  173. /** @see Zend_Log_Exception */
  174. // require_once 'Zend/Log/Exception.php';
  175. throw new Zend_Log_Exception('Invalid filter provided');
  176. }
  177. $this->_filters[] = $filter;
  178. }
  179. /**
  180. * Add a writer. A writer is responsible for taking a log
  181. * message and writing it out to storage.
  182. *
  183. * @param Zend_Log_Writer_Abstract $writer
  184. * @return void
  185. */
  186. public function addWriter(Zend_Log_Writer_Abstract $writer)
  187. {
  188. $this->_writers[] = $writer;
  189. }
  190. /**
  191. * Set an extra item to pass to the log writers.
  192. *
  193. * @param $name Name of the field
  194. * @param $value Value of the field
  195. * @return void
  196. */
  197. public function setEventItem($name, $value) {
  198. $this->_extras = array_merge($this->_extras, array($name => $value));
  199. }
  200. }