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