Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Log.php
Go to the documentation of this file.
1 <?php
40 class Zend_Log
41 {
42  const EMERG = 0; // Emergency: system is unusable
43  const ALERT = 1; // Alert: action must be taken immediately
44  const CRIT = 2; // Critical: critical conditions
45  const ERR = 3; // Error: error conditions
46  const WARN = 4; // Warning: warning conditions
47  const NOTICE = 5; // Notice: normal but significant condition
48  const INFO = 6; // Informational: informational messages
49  const DEBUG = 7; // Debug: debug messages
50 
55  protected $_priorities = array();
56 
60  protected $_writers = array();
61 
65  protected $_filters = array();
66 
70  protected $_extras = array();
71 
76  protected $_defaultWriterNamespace = 'Zend_Log_Writer';
77 
82  protected $_defaultFilterNamespace = 'Zend_Log_Filter';
83 
88  protected $_defaultFormatterNamespace = 'Zend_Log_Formatter';
89 
94  protected $_origErrorHandler = null;
95 
100  protected $_registeredErrorHandler = false;
101 
106  protected $_errorHandlerMap = false;
107 
112  protected $_timestampFormat = 'c';
113 
119  public function __construct(Zend_Log_Writer_Abstract $writer = null)
120  {
121  $r = new ReflectionClass($this);
122  $this->_priorities = array_flip($r->getConstants());
123 
124  if ($writer !== null) {
125  $this->addWriter($writer);
126  }
127  }
128 
137  static public function factory($config = array())
138  {
139  if ($config instanceof Zend_Config) {
140  $config = $config->toArray();
141  }
142 
143  if (!is_array($config) || empty($config)) {
145  #require_once 'Zend/Log/Exception.php';
146  throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config');
147  }
148 
149  if (array_key_exists('className', $config)) {
150  $class = $config['className'];
151  unset($config['className']);
152  } else {
153  $class = __CLASS__;
154  }
155 
156  $log = new $class;
157 
158  if (!$log instanceof Zend_Log) {
160  #require_once 'Zend/Log/Exception.php';
161  throw new Zend_Log_Exception('Passed className does not belong to a descendant of Zend_Log');
162  }
163 
164  if (array_key_exists('timestampFormat', $config)) {
165  if (null != $config['timestampFormat'] && '' != $config['timestampFormat']) {
166  $log->setTimestampFormat($config['timestampFormat']);
167  }
168  unset($config['timestampFormat']);
169  }
170 
171  if (!is_array(current($config))) {
172  $log->addWriter(current($config));
173  } else {
174  foreach($config as $writer) {
175  $log->addWriter($writer);
176  }
177  }
178 
179  return $log;
180  }
181 
182 
191  {
192  $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace);
193 
194  if (!$writer instanceof Zend_Log_Writer_Abstract) {
195  $writerName = is_object($writer)
196  ? get_class($writer)
197  : 'The specified writer';
199  #require_once 'Zend/Log/Exception.php';
200  throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!");
201  }
202 
203  if (isset($config['filterName'])) {
204  $filter = $this->_constructFilterFromConfig($config);
205  $writer->addFilter($filter);
206  }
207 
208  if (isset($config['formatterName'])) {
209  $formatter = $this->_constructFormatterFromConfig($config);
210  $writer->setFormatter($formatter);
211  }
212 
213  return $writer;
214  }
215 
224  {
225  $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace);
226 
227  if (!$filter instanceof Zend_Log_Filter_Interface) {
228  $filterName = is_object($filter)
229  ? get_class($filter)
230  : 'The specified filter';
232  #require_once 'Zend/Log/Exception.php';
233  throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface");
234  }
235 
236  return $filter;
237  }
238 
247  {
248  $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace);
249 
250  if (!$formatter instanceof Zend_Log_Formatter_Interface) {
251  $formatterName = is_object($formatter)
252  ? get_class($formatter)
253  : 'The specified formatter';
255  #require_once 'Zend/Log/Exception.php';
256  throw new Zend_Log_Exception($formatterName . ' does not implement Zend_Log_Formatter_Interface');
257  }
258 
259  return $formatter;
260  }
261 
271  protected function _constructFromConfig($type, $config, $namespace)
272  {
273  if ($config instanceof Zend_Config) {
274  $config = $config->toArray();
275  }
276 
277  if (!is_array($config) || empty($config)) {
278  #require_once 'Zend/Log/Exception.php';
279  throw new Zend_Log_Exception(
280  'Configuration must be an array or instance of Zend_Config'
281  );
282  }
283 
284  $params = isset($config[ $type .'Params' ]) ? $config[ $type .'Params' ] : array();
285  $className = $this->getClassName($config, $type, $namespace);
286  if (!class_exists($className)) {
287  #require_once 'Zend/Loader.php';
289  }
290 
291  $reflection = new ReflectionClass($className);
292  if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) {
293  #require_once 'Zend/Log/Exception.php';
294  throw new Zend_Log_Exception(
295  $className . ' does not implement Zend_Log_FactoryInterface and can not be constructed from config.'
296  );
297  }
298 
299  return call_user_func(array($className, 'factory'), $params);
300  }
301 
311  protected function getClassName($config, $type, $defaultNamespace)
312  {
313  if (!isset($config[$type . 'Name'])) {
314  #require_once 'Zend/Log/Exception.php';
315  throw new Zend_Log_Exception("Specify {$type}Name in the configuration array");
316  }
317 
318  $className = $config[$type . 'Name'];
319  $namespace = $defaultNamespace;
320 
321  if (isset($config[$type . 'Namespace'])) {
322  $namespace = $config[$type . 'Namespace'];
323  }
324 
325  // PHP >= 5.3.0 namespace given?
326  if (substr($namespace, -1) == '\\') {
327  return $namespace . $className;
328  }
329 
330  // empty namespace given?
331  if (strlen($namespace) === 0) {
332  return $className;
333  }
334 
335  return $namespace . '_' . $className;
336  }
337 
345  protected function _packEvent($message, $priority)
346  {
347  return array_merge(array(
348  'timestamp' => date($this->_timestampFormat),
349  'message' => $message,
350  'priority' => $priority,
351  'priorityName' => $this->_priorities[$priority]
352  ),
353  $this->_extras
354  );
355  }
356 
362  public function __destruct()
363  {
365  foreach($this->_writers as $writer) {
366  $writer->shutdown();
367  }
368  }
369 
381  public function __call($method, $params)
382  {
383  $priority = strtoupper($method);
384  if (($priority = array_search($priority, $this->_priorities)) !== false) {
385  switch (count($params)) {
386  case 0:
388  #require_once 'Zend/Log/Exception.php';
389  throw new Zend_Log_Exception('Missing log message');
390  case 1:
391  $message = array_shift($params);
392  $extras = null;
393  break;
394  default:
395  $message = array_shift($params);
396  $extras = array_shift($params);
397  break;
398  }
399  $this->log($message, $priority, $extras);
400  } else {
402  #require_once 'Zend/Log/Exception.php';
403  throw new Zend_Log_Exception('Bad log priority');
404  }
405  }
406 
416  public function log($message, $priority, $extras = null)
417  {
418  // sanity checks
419  if (empty($this->_writers)) {
421  #require_once 'Zend/Log/Exception.php';
422  throw new Zend_Log_Exception('No writers were added');
423  }
424 
425  if (! isset($this->_priorities[$priority])) {
427  #require_once 'Zend/Log/Exception.php';
428  throw new Zend_Log_Exception('Bad log priority');
429  }
430 
431  // pack into event required by filters and writers
432  $event = $this->_packEvent($message, $priority);
433 
434  // Check to see if any extra information was passed
435  if (!empty($extras)) {
436  $info = array();
437  if (is_array($extras)) {
438  foreach ($extras as $key => $value) {
439  if (is_string($key)) {
440  $event[$key] = $value;
441  } else {
442  $info[] = $value;
443  }
444  }
445  } else {
446  $info = $extras;
447  }
448  if (!empty($info)) {
449  $event['info'] = $info;
450  }
451  }
452 
453  // abort if rejected by the global filters
455  foreach ($this->_filters as $filter) {
456  if (! $filter->accept($event)) {
457  return;
458  }
459  }
460 
461  // send to each writer
463  foreach ($this->_writers as $writer) {
464  $writer->write($event);
465  }
466  }
467 
476  public function addPriority($name, $priority)
477  {
478  // Priority names must be uppercase for predictability.
479  $name = strtoupper($name);
480 
481  if (isset($this->_priorities[$priority])
482  || false !== array_search($name, $this->_priorities)) {
484  #require_once 'Zend/Log/Exception.php';
485  throw new Zend_Log_Exception('Existing priorities cannot be overwritten');
486  }
487 
488  $this->_priorities[$priority] = $name;
489  return $this;
490  }
491 
501  public function addFilter($filter)
502  {
503  if (is_int($filter)) {
505  #require_once 'Zend/Log/Filter/Priority.php';
506  $filter = new Zend_Log_Filter_Priority($filter);
507 
508  } elseif ($filter instanceof Zend_Config || is_array($filter)) {
509  $filter = $this->_constructFilterFromConfig($filter);
510 
511  } elseif(! $filter instanceof Zend_Log_Filter_Interface) {
513  #require_once 'Zend/Log/Exception.php';
514  throw new Zend_Log_Exception('Invalid filter provided');
515  }
516 
517  $this->_filters[] = $filter;
518  return $this;
519  }
520 
529  public function addWriter($writer)
530  {
531  if (is_array($writer) || $writer instanceof Zend_Config) {
532  $writer = $this->_constructWriterFromConfig($writer);
533  }
534 
535  if (!$writer instanceof Zend_Log_Writer_Abstract) {
537  #require_once 'Zend/Log/Exception.php';
538  throw new Zend_Log_Exception(
539  'Writer must be an instance of Zend_Log_Writer_Abstract'
540  . ' or you should pass a configuration array'
541  );
542  }
543 
544  $this->_writers[] = $writer;
545  return $this;
546  }
547 
555  public function setEventItem($name, $value)
556  {
557  $this->_extras = array_merge($this->_extras, array($name => $value));
558  return $this;
559  }
560 
576  public function registerErrorHandler()
577  {
578  // Only register once. Avoids loop issues if it gets registered twice.
579  if ($this->_registeredErrorHandler) {
580  return $this;
581  }
582 
583  $this->_origErrorHandler = set_error_handler(array($this, 'errorHandler'));
584 
585  // Contruct a default map of phpErrors to Zend_Log priorities.
586  // Some of the errors are uncatchable, but are included for completeness
587  $this->_errorHandlerMap = array(
588  E_NOTICE => Zend_Log::NOTICE,
589  E_USER_NOTICE => Zend_Log::NOTICE,
590  E_WARNING => Zend_Log::WARN,
591  E_CORE_WARNING => Zend_Log::WARN,
592  E_USER_WARNING => Zend_Log::WARN,
593  E_ERROR => Zend_Log::ERR,
594  E_USER_ERROR => Zend_Log::ERR,
595  E_CORE_ERROR => Zend_Log::ERR,
596  E_RECOVERABLE_ERROR => Zend_Log::ERR,
597  E_STRICT => Zend_Log::DEBUG,
598  );
599  // PHP 5.3.0+
600  if (defined('E_DEPRECATED')) {
601  $this->_errorHandlerMap['E_DEPRECATED'] = Zend_Log::DEBUG;
602  }
603  if (defined('E_USER_DEPRECATED')) {
604  $this->_errorHandlerMap['E_USER_DEPRECATED'] = Zend_Log::DEBUG;
605  }
606 
607  $this->_registeredErrorHandler = true;
608  return $this;
609  }
610 
622  public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
623  {
624  $errorLevel = error_reporting();
625 
626  if ($errorLevel & $errno) {
627  if (isset($this->_errorHandlerMap[$errno])) {
628  $priority = $this->_errorHandlerMap[$errno];
629  } else {
630  $priority = Zend_Log::INFO;
631  }
632  $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext));
633  }
634 
635  if ($this->_origErrorHandler !== null) {
636  return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
637  }
638  return false;
639  }
640 
647  public function setTimestampFormat($format)
648  {
649  $this->_timestampFormat = $format;
650  return $this;
651  }
652 
658  public function getTimestampFormat()
659  {
661  }
662 }
$_defaultFormatterNamespace
Definition: Log.php:88
setEventItem($name, $value)
Definition: Log.php:555
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$_writers
Definition: Log.php:60
const WARN
Definition: Log.php:46
static loadClass($class, $dirs=null)
Definition: Loader.php:52
addFilter($filter)
Definition: Log.php:501
$_filters
Definition: Log.php:65
$config
Definition: fraud_order.php:17
$_errorHandlerMap
Definition: Log.php:106
const DEBUG
Definition: Log.php:49
$_defaultFilterNamespace
Definition: Log.php:82
errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
Definition: Log.php:622
const CRIT
Definition: Log.php:44
const ERR
Definition: Log.php:45
$_registeredErrorHandler
Definition: Log.php:100
$message
$_priorities
Definition: Log.php:55
$type
Definition: item.phtml:13
const INFO
Definition: Log.php:48
registerErrorHandler()
Definition: Log.php:576
_constructFormatterFromConfig($config)
Definition: Log.php:246
getTimestampFormat()
Definition: Log.php:658
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
const EMERG
Definition: Log.php:42
_constructFilterFromConfig($config)
Definition: Log.php:223
const ALERT
Definition: Log.php:43
$_extras
Definition: Log.php:70
__construct(Zend_Log_Writer_Abstract $writer=null)
Definition: Log.php:119
$method
Definition: info.phtml:13
$_defaultWriterNamespace
Definition: Log.php:76
_constructFromConfig($type, $config, $namespace)
Definition: Log.php:271
__call($method, $params)
Definition: Log.php:381
static factory($config=array())
Definition: Log.php:137
const NOTICE
Definition: Log.php:47
setTimestampFormat($format)
Definition: Log.php:647
$_origErrorHandler
Definition: Log.php:94
$_timestampFormat
Definition: Log.php:112
getClassName($config, $type, $defaultNamespace)
Definition: Log.php:311
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
_packEvent($message, $priority)
Definition: Log.php:345
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
addPriority($name, $priority)
Definition: Log.php:476
_constructWriterFromConfig($config)
Definition: Log.php:190
addWriter($writer)
Definition: Log.php:529
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14