PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Log.php

https://github.com/qmmr/iAlbums
PHP | 617 lines | 308 code | 68 blank | 241 comment | 45 complexity | 68ba0c66c8b7ade10d0c81442703f2ec 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-2010 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 23651 2011-01-21 21:51:00Z mikaelkael $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Log
  24. * @copyright Copyright (c) 2005-2010 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 23651 2011-01-21 21:51:00Z mikaelkael $
  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. *
  57. * @var string
  58. */
  59. protected $_defaultWriterNamespace = 'Zend_Log_Writer';
  60. /**
  61. *
  62. * @var string
  63. */
  64. protected $_defaultFilterNamespace = 'Zend_Log_Filter';
  65. /**
  66. *
  67. * @var string
  68. */
  69. protected $_defaultFormatterNamespace = 'Zend_Log_Formatter';
  70. /**
  71. *
  72. * @var callback
  73. */
  74. protected $_origErrorHandler = null;
  75. /**
  76. *
  77. * @var boolean
  78. */
  79. protected $_registeredErrorHandler = false;
  80. /**
  81. *
  82. * @var array|boolean
  83. */
  84. protected $_errorHandlerMap = false;
  85. /**
  86. *
  87. * @var string
  88. */
  89. protected $_timestampFormat = 'c';
  90. /**
  91. * Class constructor. Create a new logger
  92. *
  93. * @param Zend_Log_Writer_Abstract|null $writer default writer
  94. * @return void
  95. */
  96. public function __construct(Zend_Log_Writer_Abstract $writer = null)
  97. {
  98. $r = new ReflectionClass($this);
  99. $this->_priorities = array_flip($r->getConstants());
  100. if ($writer !== null) {
  101. $this->addWriter($writer);
  102. }
  103. }
  104. /**
  105. * Factory to construct the logger and one or more writers
  106. * based on the configuration array
  107. *
  108. * @param array|Zend_Config Array or instance of Zend_Config
  109. * @return Zend_Log
  110. * @throws Zend_Log_Exception
  111. */
  112. static public function factory($config = array())
  113. {
  114. if ($config instanceof Zend_Config) {
  115. $config = $config->toArray();
  116. }
  117. if (!is_array($config) || empty($config)) {
  118. /** @see Zend_Log_Exception */
  119. require_once 'Zend/Log/Exception.php';
  120. throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config');
  121. }
  122. $log = new self;
  123. if (!is_array(current($config))) {
  124. $log->addWriter(current($config));
  125. } else {
  126. foreach($config as $writer) {
  127. $log->addWriter($writer);
  128. }
  129. }
  130. return $log;
  131. }
  132. /**
  133. * Construct a writer object based on a configuration array
  134. *
  135. * @param array $spec config array with writer spec
  136. * @return Zend_Log_Writer_Abstract
  137. * @throws Zend_Log_Exception
  138. */
  139. protected function _constructWriterFromConfig($config)
  140. {
  141. $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace);
  142. if (!$writer instanceof Zend_Log_Writer_Abstract) {
  143. $writerName = is_object($writer)
  144. ? get_class($writer)
  145. : 'The specified writer';
  146. /** @see Zend_Log_Exception */
  147. require_once 'Zend/Log/Exception.php';
  148. throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!");
  149. }
  150. if (isset($config['filterName'])) {
  151. $filter = $this->_constructFilterFromConfig($config);
  152. $writer->addFilter($filter);
  153. }
  154. if (isset($config['formatterName'])) {
  155. $formatter = $this->_constructFormatterFromConfig($config);
  156. $writer->setFormatter($formatter);
  157. }
  158. return $writer;
  159. }
  160. /**
  161. * Construct filter object from configuration array or Zend_Config object
  162. *
  163. * @param array|Zend_Config $config Zend_Config or Array
  164. * @return Zend_Log_Filter_Interface
  165. * @throws Zend_Log_Exception
  166. */
  167. protected function _constructFilterFromConfig($config)
  168. {
  169. $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace);
  170. if (!$filter instanceof Zend_Log_Filter_Interface) {
  171. $filterName = is_object($filter)
  172. ? get_class($filter)
  173. : 'The specified filter';
  174. /** @see Zend_Log_Exception */
  175. require_once 'Zend/Log/Exception.php';
  176. throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface");
  177. }
  178. return $filter;
  179. }
  180. /**
  181. * Construct formatter object from configuration array or Zend_Config object
  182. *
  183. * @param array|Zend_Config $config Zend_Config or Array
  184. * @return Zend_Log_Formatter_Interface
  185. * @throws Zend_Log_Exception
  186. */
  187. protected function _constructFormatterFromConfig($config)
  188. {
  189. $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace);
  190. if (!$formatter instanceof Zend_Log_Formatter_Interface) {
  191. $formatterName = is_object($formatter)
  192. ? get_class($formatter)
  193. : 'The specified formatter';
  194. /** @see Zend_Log_Exception */
  195. require_once 'Zend/Log/Exception.php';
  196. throw new Zend_Log_Exception($formatterName . ' does not implement Zend_Log_Formatter_Interface');
  197. }
  198. return $formatter;
  199. }
  200. /**
  201. * Construct a filter or writer from config
  202. *
  203. * @param string $type 'writer' of 'filter'
  204. * @param mixed $config Zend_Config or Array
  205. * @param string $namespace
  206. * @return object
  207. * @throws Zend_Log_Exception
  208. */
  209. protected function _constructFromConfig($type, $config, $namespace)
  210. {
  211. if ($config instanceof Zend_Config) {
  212. $config = $config->toArray();
  213. }
  214. if (!is_array($config) || empty($config)) {
  215. require_once 'Zend/Log/Exception.php';
  216. throw new Zend_Log_Exception(
  217. 'Configuration must be an array or instance of Zend_Config'
  218. );
  219. }
  220. $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array();
  221. $className = $this->getClassName($config, $type, $namespace);
  222. if (!class_exists($className)) {
  223. require_once 'Zend/Loader.php';
  224. Zend_Loader::loadClass($className);
  225. }
  226. $reflection = new ReflectionClass($className);
  227. if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) {
  228. require_once 'Zend/Log/Exception.php';
  229. throw new Zend_Log_Exception(
  230. $className . ' does not implement Zend_Log_FactoryInterface and can not be constructed from config.'
  231. );
  232. }
  233. return call_user_func(array($className, 'factory'), $params);
  234. }
  235. /**
  236. * Get the writer or filter full classname
  237. *
  238. * @param array $config
  239. * @param string $type filter|writer
  240. * @param string $defaultNamespace
  241. * @return string full classname
  242. * @throws Zend_Log_Exception
  243. */
  244. protected function getClassName($config, $type, $defaultNamespace)
  245. {
  246. if (!isset($config[ $type . 'Name' ])) {
  247. require_once 'Zend/Log/Exception.php';
  248. throw new Zend_Log_Exception("Specify {$type}Name in the configuration array");
  249. }
  250. $className = $config[ $type . 'Name' ];
  251. $namespace = $defaultNamespace;
  252. if (isset($config[ $type . 'Namespace' ])) {
  253. $namespace = $config[ $type . 'Namespace' ];
  254. }
  255. $fullClassName = $namespace . '_' . $className;
  256. return $fullClassName;
  257. }
  258. /**
  259. * Packs message and priority into Event array
  260. *
  261. * @param string $message Message to log
  262. * @param integer $priority Priority of message
  263. * @return array Event array
  264. */
  265. protected function _packEvent($message, $priority)
  266. {
  267. return array_merge(array(
  268. 'timestamp' => date($this->_timestampFormat),
  269. 'message' => $message,
  270. 'priority' => $priority,
  271. 'priorityName' => $this->_priorities[$priority]
  272. ),
  273. $this->_extras
  274. );
  275. }
  276. /**
  277. * Class destructor. Shutdown log writers
  278. *
  279. * @return void
  280. */
  281. public function __destruct()
  282. {
  283. foreach($this->_writers as $writer) {
  284. $writer->shutdown();
  285. }
  286. }
  287. /**
  288. * Undefined method handler allows a shortcut:
  289. * $log->priorityName('message')
  290. * instead of
  291. * $log->log('message', Zend_Log::PRIORITY_NAME)
  292. *
  293. * @param string $method priority name
  294. * @param string $params message to log
  295. * @return void
  296. * @throws Zend_Log_Exception
  297. */
  298. public function __call($method, $params)
  299. {
  300. $priority = strtoupper($method);
  301. if (($priority = array_search($priority, $this->_priorities)) !== false) {
  302. switch (count($params)) {
  303. case 0:
  304. /** @see Zend_Log_Exception */
  305. require_once 'Zend/Log/Exception.php';
  306. throw new Zend_Log_Exception('Missing log message');
  307. case 1:
  308. $message = array_shift($params);
  309. $extras = null;
  310. break;
  311. default:
  312. $message = array_shift($params);
  313. $extras = array_shift($params);
  314. break;
  315. }
  316. $this->log($message, $priority, $extras);
  317. } else {
  318. /** @see Zend_Log_Exception */
  319. require_once 'Zend/Log/Exception.php';
  320. throw new Zend_Log_Exception('Bad log priority');
  321. }
  322. }
  323. /**
  324. * Log a message at a priority
  325. *
  326. * @param string $message Message to log
  327. * @param integer $priority Priority of message
  328. * @param mixed $extras Extra information to log in event
  329. * @return void
  330. * @throws Zend_Log_Exception
  331. */
  332. public function log($message, $priority, $extras = null)
  333. {
  334. // sanity checks
  335. if (empty($this->_writers)) {
  336. /** @see Zend_Log_Exception */
  337. require_once 'Zend/Log/Exception.php';
  338. throw new Zend_Log_Exception('No writers were added');
  339. }
  340. if (! isset($this->_priorities[$priority])) {
  341. /** @see Zend_Log_Exception */
  342. require_once 'Zend/Log/Exception.php';
  343. throw new Zend_Log_Exception('Bad log priority');
  344. }
  345. // pack into event required by filters and writers
  346. $event = $this->_packEvent($message, $priority);
  347. // Check to see if any extra information was passed
  348. if (!empty($extras)) {
  349. $info = array();
  350. if (is_array($extras)) {
  351. foreach ($extras as $key => $value) {
  352. if (is_string($key)) {
  353. $event[$key] = $value;
  354. } else {
  355. $info[] = $value;
  356. }
  357. }
  358. } else {
  359. $info = $extras;
  360. }
  361. if (!empty($info)) {
  362. $event['info'] = $info;
  363. }
  364. }
  365. // abort if rejected by the global filters
  366. foreach ($this->_filters as $filter) {
  367. if (! $filter->accept($event)) {
  368. return;
  369. }
  370. }
  371. // send to each writer
  372. foreach ($this->_writers as $writer) {
  373. $writer->write($event);
  374. }
  375. }
  376. /**
  377. * Add a custom priority
  378. *
  379. * @param string $name Name of priority
  380. * @param integer $priority Numeric priority
  381. * @throws Zend_Log_Exception
  382. */
  383. public function addPriority($name, $priority)
  384. {
  385. // Priority names must be uppercase for predictability.
  386. $name = strtoupper($name);
  387. if (isset($this->_priorities[$priority])
  388. || false !== array_search($name, $this->_priorities)) {
  389. /** @see Zend_Log_Exception */
  390. require_once 'Zend/Log/Exception.php';
  391. throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
  392. }
  393. $this->_priorities[$priority] = $name;
  394. return $this;
  395. }
  396. /**
  397. * Add a filter that will be applied before all log writers.
  398. * Before a message will be received by any of the writers, it
  399. * must be accepted by all filters added with this method.
  400. *
  401. * @param int|Zend_Config|array|Zend_Log_Filter_Interface $filter
  402. * @return Zend_Log
  403. * @throws Zend_Log_Exception
  404. */
  405. public function addFilter($filter)
  406. {
  407. if (is_int($filter)) {
  408. /** @see Zend_Log_Filter_Priority */
  409. require_once 'Zend/Log/Filter/Priority.php';
  410. $filter = new Zend_Log_Filter_Priority($filter);
  411. } elseif ($filter instanceof Zend_Config || is_array($filter)) {
  412. $filter = $this->_constructFilterFromConfig($filter);
  413. } elseif(! $filter instanceof Zend_Log_Filter_Interface) {
  414. /** @see Zend_Log_Exception */
  415. require_once 'Zend/Log/Exception.php';
  416. throw new Zend_Log_Exception('Invalid filter provided');
  417. }
  418. $this->_filters[] = $filter;
  419. return $this;
  420. }
  421. /**
  422. * Add a writer. A writer is responsible for taking a log
  423. * message and writing it out to storage.
  424. *
  425. * @param mixed $writer Zend_Log_Writer_Abstract or Config array
  426. * @return Zend_Log
  427. */
  428. public function addWriter($writer)
  429. {
  430. if (is_array($writer) || $writer instanceof Zend_Config) {
  431. $writer = $this->_constructWriterFromConfig($writer);
  432. }
  433. if (!$writer instanceof Zend_Log_Writer_Abstract) {
  434. /** @see Zend_Log_Exception */
  435. require_once 'Zend/Log/Exception.php';
  436. throw new Zend_Log_Exception(
  437. 'Writer must be an instance of Zend_Log_Writer_Abstract'
  438. . ' or you should pass a configuration array'
  439. );
  440. }
  441. $this->_writers[] = $writer;
  442. return $this;
  443. }
  444. /**
  445. * Set an extra item to pass to the log writers.
  446. *
  447. * @param string $name Name of the field
  448. * @param string $value Value of the field
  449. * @return Zend_Log
  450. */
  451. public function setEventItem($name, $value)
  452. {
  453. $this->_extras = array_merge($this->_extras, array($name => $value));
  454. return $this;
  455. }
  456. /**
  457. * Register Logging system as an error handler to log php errors
  458. * Note: it still calls the original error handler if set_error_handler is able to return it.
  459. *
  460. * Errors will be mapped as:
  461. * E_NOTICE, E_USER_NOTICE => NOTICE
  462. * E_WARNING, E_CORE_WARNING, E_USER_WARNING => WARN
  463. * E_ERROR, E_USER_ERROR, E_CORE_ERROR, E_RECOVERABLE_ERROR => ERR
  464. * E_DEPRECATED, E_STRICT, E_USER_DEPRECATED => DEBUG
  465. * (unknown/other) => INFO
  466. *
  467. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  468. *
  469. * @return Zend_Log
  470. */
  471. public function registerErrorHandler()
  472. {
  473. // Only register once. Avoids loop issues if it gets registered twice.
  474. if ($this->_registeredErrorHandler) {
  475. return $this;
  476. }
  477. $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler'));
  478. // Contruct a default map of phpErrors to Zend_Log priorities.
  479. // Some of the errors are uncatchable, but are included for completeness
  480. $this->_errorHandlerMap = array(
  481. E_NOTICE => Zend_Log::NOTICE,
  482. E_USER_NOTICE => Zend_Log::NOTICE,
  483. E_WARNING => Zend_Log::WARN,
  484. E_CORE_WARNING => Zend_Log::WARN,
  485. E_USER_WARNING => Zend_Log::WARN,
  486. E_ERROR => Zend_Log::ERR,
  487. E_USER_ERROR => Zend_Log::ERR,
  488. E_CORE_ERROR => Zend_Log::ERR,
  489. E_RECOVERABLE_ERROR => Zend_Log::ERR,
  490. E_STRICT => Zend_Log::DEBUG,
  491. );
  492. // PHP 5.3.0+
  493. if (defined('E_DEPRECATED')) {
  494. $this->_errorHandlerMap['E_DEPRECATED'] = Zend_Log::DEBUG;
  495. }
  496. if (defined('E_USER_DEPRECATED')) {
  497. $this->_errorHandlerMap['E_USER_DEPRECATED'] = Zend_Log::DEBUG;
  498. }
  499. $this->_registeredErrorHandler = true;
  500. return $this;
  501. }
  502. /**
  503. * Error Handler will convert error into log message, and then call the original error handler
  504. *
  505. * @link http://www.php.net/manual/en/function.set-error-handler.php Custom error handler
  506. * @param int $errno
  507. * @param string $errstr
  508. * @param string $errfile
  509. * @param int $errline
  510. * @param array $errcontext
  511. * @return boolean
  512. */
  513. public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  514. {
  515. $errorLevel = error_reporting();
  516. if ($errorLevel && $errno) {
  517. if (isset($this->_errorHandlerMap[$errno])) {
  518. $priority = $this->_errorHandlerMap[$errno];
  519. } else {
  520. $priority = Zend_Log::INFO;
  521. }
  522. $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext));
  523. }
  524. if ($this->_origErrorHandler !== null) {
  525. return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
  526. }
  527. return false;
  528. }
  529. /**
  530. * Set timestamp format for log entries.
  531. *
  532. * @param string $format
  533. * @return Zend_Log
  534. */
  535. public function setTimestampFormat($format)
  536. {
  537. $this->_timestampFormat = $format;
  538. return $this;
  539. }
  540. /**
  541. * Get timestamp format used for log entries.
  542. *
  543. * @return string
  544. */
  545. public function getTimestampFormat()
  546. {
  547. return $this->_timestampFormat;
  548. }
  549. }