PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Log.php

https://bitbucket.org/baruffaldi/website-insaneminds
PHP | 211 lines | 85 code | 23 blank | 103 comment | 9 complexity | 6f0571f638124a1ef7831d72e68ec7d2 MD5 | raw file
  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 8064 2008-02-16 10:58:39Z thomas $
  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 8064 2008-02-16 10:58:39Z thomas $
  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($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. 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 = 7 )
  110. {
  111. // sanity checks
  112. if (empty($this->_writers)) {
  113. throw new Zend_Log_Exception('No writers were added');
  114. }
  115. if (! isset($this->_priorities[$priority])) {
  116. throw new Zend_Log_Exception('Bad log priority');
  117. }
  118. // pack into event required by filters and writers
  119. $event = array_merge(array( 'timestamp' => date('c'),
  120. 'message' => $message,
  121. 'priority' => $priority,
  122. 'priorityName' => $this->_priorities[$priority]),
  123. $this->_extras);
  124. // abort if rejected by the global filters
  125. foreach ($this->_filters as $filter) {
  126. if (! $filter->accept($event)) {
  127. return;
  128. }
  129. }
  130. // send to each writer
  131. foreach ($this->_writers as $writer) {
  132. $writer->write($event);
  133. }
  134. }
  135. /**
  136. * Add a custom priority
  137. *
  138. * @param string $name Name of priority
  139. * @param integer $priority Numeric priority
  140. * @throws Zend_Log_InvalidArgumentException
  141. */
  142. public function addPriority($name, $priority)
  143. {
  144. // Priority names must be uppercase for predictability.
  145. $name = strtoupper($name);
  146. if (isset($this->_priorities[$priority])
  147. || array_search($name, $this->_priorities)) {
  148. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  149. }
  150. $this->_priorities[$priority] = $name;
  151. }
  152. /**
  153. * Add a filter that will be applied before all log writers.
  154. * Before a message will be received by any of the writers, it
  155. * must be accepted by all filters added with this method.
  156. *
  157. * @param Zend_Log_Filter_Interface $filter
  158. * @return void
  159. */
  160. public function addFilter($filter)
  161. {
  162. if (is_integer($filter)) {
  163. $filter = new Zend_Log_Filter_Priority($filter);
  164. }
  165. $this->_filters[] = $filter;
  166. }
  167. /**
  168. * Add a writer. A writer is responsible for taking a log
  169. * message and writing it out to storage.
  170. *
  171. * @param Zend_Log_Writer_Abstract $writer
  172. * @return void
  173. */
  174. public function addWriter($writer)
  175. {
  176. $this->_writers[] = $writer;
  177. }
  178. /**
  179. * Set an extra item to pass to the log writers.
  180. *
  181. * @param $name Name of the field
  182. * @param $value Value of the field
  183. * @return void
  184. */
  185. public function setEventItem($name, $value) {
  186. $this->_extras = array_merge($this->_extras, array($name => $value));
  187. }
  188. }