Log.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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$
  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$
  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. switch (count($params)) {
  95. case 0:
  96. /** @see Zend_Log_Exception */
  97. require_once 'Zend/Log/Exception.php';
  98. throw new Zend_Log_Exception('Missing log message');
  99. case 1:
  100. $message = array_shift($params);
  101. $extras = null;
  102. break;
  103. default:
  104. $message = array_shift($params);
  105. $extras = array_shift($params);
  106. break;
  107. }
  108. $this->log($message, $priority, $extras);
  109. } else {
  110. /** @see Zend_Log_Exception */
  111. require_once 'Zend/Log/Exception.php';
  112. throw new Zend_Log_Exception('Bad log priority');
  113. }
  114. }
  115. /**
  116. * Log a message at a priority
  117. *
  118. * @param string $message Message to log
  119. * @param integer $priority Priority of message
  120. * @param mixed $extras Extra information to log in event
  121. * @return void
  122. * @throws Zend_Log_Exception
  123. */
  124. public function log($message, $priority, $extras = null)
  125. {
  126. // sanity checks
  127. if (empty($this->_writers)) {
  128. /** @see Zend_Log_Exception */
  129. require_once 'Zend/Log/Exception.php';
  130. throw new Zend_Log_Exception('No writers were added');
  131. }
  132. if (! isset($this->_priorities[$priority])) {
  133. /** @see Zend_Log_Exception */
  134. require_once 'Zend/Log/Exception.php';
  135. throw new Zend_Log_Exception('Bad log priority');
  136. }
  137. // pack into event required by filters and writers
  138. $event = array_merge(array('timestamp' => date('c'),
  139. 'message' => $message,
  140. 'priority' => $priority,
  141. 'priorityName' => $this->_priorities[$priority]),
  142. $this->_extras);
  143. // Check to see if any extra information was passed
  144. if (!empty($extras)) {
  145. $info = array();
  146. if (is_array($extras)) {
  147. foreach ($extras as $key => $value) {
  148. if (is_string($key)) {
  149. $event[$key] = $value;
  150. } else {
  151. $info[] = $value;
  152. }
  153. }
  154. } else {
  155. $info = $extras;
  156. }
  157. if (!empty($info)) {
  158. $event['info'] = $info;
  159. }
  160. }
  161. // abort if rejected by the global filters
  162. foreach ($this->_filters as $filter) {
  163. if (! $filter->accept($event)) {
  164. return;
  165. }
  166. }
  167. // send to each writer
  168. foreach ($this->_writers as $writer) {
  169. $writer->write($event);
  170. }
  171. }
  172. /**
  173. * Add a custom priority
  174. *
  175. * @param string $name Name of priority
  176. * @param integer $priority Numeric priority
  177. * @throws Zend_Log_InvalidArgumentException
  178. */
  179. public function addPriority($name, $priority)
  180. {
  181. // Priority names must be uppercase for predictability.
  182. $name = strtoupper($name);
  183. if (isset($this->_priorities[$priority])
  184. || array_search($name, $this->_priorities)) {
  185. /** @see Zend_Log_Exception */
  186. require_once 'Zend/Log/Exception.php';
  187. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  188. }
  189. $this->_priorities[$priority] = $name;
  190. }
  191. /**
  192. * Add a filter that will be applied before all log writers.
  193. * Before a message will be received by any of the writers, it
  194. * must be accepted by all filters added with this method.
  195. *
  196. * @param int|Zend_Log_Filter_Interface $filter
  197. * @return void
  198. */
  199. public function addFilter($filter)
  200. {
  201. if (is_integer($filter)) {
  202. /** @see Zend_Log_Filter_Priority */
  203. require_once 'Zend/Log/Filter/Priority.php';
  204. $filter = new Zend_Log_Filter_Priority($filter);
  205. } elseif(!is_object($filter) || ! $filter instanceof Zend_Log_Filter_Interface) {
  206. /** @see Zend_Log_Exception */
  207. require_once 'Zend/Log/Exception.php';
  208. throw new Zend_Log_Exception('Invalid filter provided');
  209. }
  210. $this->_filters[] = $filter;
  211. }
  212. /**
  213. * Add a writer. A writer is responsible for taking a log
  214. * message and writing it out to storage.
  215. *
  216. * @param Zend_Log_Writer_Abstract $writer
  217. * @return void
  218. */
  219. public function addWriter(Zend_Log_Writer_Abstract $writer)
  220. {
  221. $this->_writers[] = $writer;
  222. }
  223. /**
  224. * Set an extra item to pass to the log writers.
  225. *
  226. * @param $name Name of the field
  227. * @param $value Value of the field
  228. * @return void
  229. */
  230. public function setEventItem($name, $value) {
  231. $this->_extras = array_merge($this->_extras, array($name => $value));
  232. }
  233. }