PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Zend/Log.php

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