PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/src/Zend/Log.php

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