PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log.php

https://bitbucket.org/baruffaldi/webapp-urltube
PHP | 223 lines | 92 code | 23 blank | 108 comment | 10 complexity | b4fd5aa7a0d3b14fa7647c7650a6c611 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2008 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 9382 2008-05-05 18:55:55Z doctorrock83 $
  20. */
  21. /** Zend_Log_Filter_Priority */
  22. require_once 'Zend/Log/Filter/Priority.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Log
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. * @version $Id: Log.php 9382 2008-05-05 18:55:55Z doctorrock83 $
  29. */
  30. class Zend_Log
  31. {
  32. const EMERG = 0; // Emergency: system is unusable
  33. const ALERT = 1; // Alert: action must be taken immediately
  34. const CRIT = 2; // Critical: critical conditions
  35. const ERR = 3; // Error: error conditions
  36. const WARN = 4; // Warning: warning conditions
  37. const NOTICE = 5; // Notice: normal but significant condition
  38. const INFO = 6; // Informational: informational messages
  39. const DEBUG = 7; // Debug: debug messages
  40. /**
  41. * @var array of priorities where the keys are the
  42. * priority numbers and the values are the priority names
  43. */
  44. protected $_priorities = array();
  45. /**
  46. * @var array of Zend_Log_Writer_Abstract
  47. */
  48. protected $_writers = array();
  49. /**
  50. * @var array of Zend_Log_Filter_Interface
  51. */
  52. protected $_filters = array();
  53. /**
  54. * @var array of extra log event
  55. */
  56. protected $_extras = array();
  57. /**
  58. * Class constructor. Create a new logger
  59. *
  60. * @param Zend_Log_Writer_Abstract|null $writer default writer
  61. */
  62. public function __construct(Zend_Log_Writer_Abstract $writer = null)
  63. {
  64. $r = new ReflectionClass($this);
  65. $this->_priorities = array_flip($r->getConstants());
  66. if ($writer !== null) {
  67. $this->addWriter($writer);
  68. }
  69. }
  70. /**
  71. * Class destructor. Shutdown log writers
  72. *
  73. * @return void
  74. */
  75. public function __destruct()
  76. {
  77. foreach($this->_writers as $writer) {
  78. $writer->shutdown();
  79. }
  80. }
  81. /**
  82. * Undefined method handler allows a shortcut:
  83. * $log->priorityName('message')
  84. * instead of
  85. * $log->log('message', Zend_Log::PRIORITY_NAME)
  86. *
  87. * @param string $method priority name
  88. * @param string $params message to log
  89. * @return void
  90. * @throws Zend_Log_Exception
  91. */
  92. public function __call($method, $params)
  93. {
  94. $priority = strtoupper($method);
  95. if (($priority = array_search($priority, $this->_priorities)) !== false) {
  96. $this->log(array_shift($params), $priority);
  97. } else {
  98. /** @see Zend_Log_Exception */
  99. require_once 'Zend/Log/Exception.php';
  100. throw new Zend_Log_Exception('Bad log priority');
  101. }
  102. }
  103. /**
  104. * Log a message at a priority
  105. *
  106. * @param string $message Message to log
  107. * @param integer $priority Priority of message
  108. * @return void
  109. * @throws Zend_Log_Exception
  110. */
  111. public function log($message, $priority)
  112. {
  113. // sanity checks
  114. if (empty($this->_writers)) {
  115. /** @see Zend_Log_Exception */
  116. require_once 'Zend/Log/Exception.php';
  117. throw new Zend_Log_Exception('No writers were added');
  118. }
  119. if (! isset($this->_priorities[$priority])) {
  120. /** @see Zend_Log_Exception */
  121. require_once 'Zend/Log/Exception.php';
  122. throw new Zend_Log_Exception('Bad log priority');
  123. }
  124. // pack into event required by filters and writers
  125. $event = array_merge(array('timestamp' => date('c'),
  126. 'message' => $message,
  127. 'priority' => $priority,
  128. 'priorityName' => $this->_priorities[$priority]),
  129. $this->_extras);
  130. // abort if rejected by the global filters
  131. foreach ($this->_filters as $filter) {
  132. if (! $filter->accept($event)) {
  133. return;
  134. }
  135. }
  136. // send to each writer
  137. foreach ($this->_writers as $writer) {
  138. $writer->write($event);
  139. }
  140. }
  141. /**
  142. * Add a custom priority
  143. *
  144. * @param string $name Name of priority
  145. * @param integer $priority Numeric priority
  146. * @throws Zend_Log_InvalidArgumentException
  147. */
  148. public function addPriority($name, $priority)
  149. {
  150. // Priority names must be uppercase for predictability.
  151. $name = strtoupper($name);
  152. if (isset($this->_priorities[$priority])
  153. || array_search($name, $this->_priorities)) {
  154. /** @see Zend_Log_Exception */
  155. require_once 'Zend/Log/Exception.php';
  156. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  157. }
  158. $this->_priorities[$priority] = $name;
  159. }
  160. /**
  161. * Add a filter that will be applied before all log writers.
  162. * Before a message will be received by any of the writers, it
  163. * must be accepted by all filters added with this method.
  164. *
  165. * @param int|Zend_Log_Filter_Interface $filter
  166. * @return void
  167. */
  168. public function addFilter($filter)
  169. {
  170. if (is_integer($filter)) {
  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. }