|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Logger |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Logger\Handler; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Logger\LogLevel; |
|
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
|
19
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
|
20
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
|
21
|
|
|
use Phossa2\Logger\Formatter\FormatterAwareTrait; |
|
22
|
|
|
use Phossa2\Logger\Formatter\FormatterAwareInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* HandlerAbstract |
|
26
|
|
|
* |
|
27
|
|
|
* @package Phossa2\Logger |
|
28
|
|
|
* @author Hong Zhang <[email protected]> |
|
29
|
|
|
* @see ObjectAbstract |
|
30
|
|
|
* @see HandlerInterface |
|
31
|
|
|
* @version 2.0.0 |
|
32
|
|
|
* @since 2.0.0 added |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class HandlerAbstract extends ObjectAbstract implements HandlerInterface, FormatterAwareInterface |
|
35
|
|
|
{ |
|
36
|
|
|
use FormatterAwareTrait; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
* @access protected |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $level; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Stop log propagation |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
* @access protected |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $stop; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Created with level handling |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $logLevel |
|
|
|
|
|
|
56
|
|
|
* @param FormatterInterface $formatter |
|
57
|
|
|
* @param bool $stopPropagation |
|
58
|
|
|
* @access protected |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct( |
|
61
|
|
|
/*# string */ $level = LogLevel::WARNING, |
|
62
|
|
|
FormatterInterface $formatter = null, |
|
63
|
|
|
/*# bool */ $stopPropagation = false |
|
64
|
|
|
) { |
|
65
|
|
|
$this->level = $level; |
|
66
|
|
|
$this->stop = (bool) $stopPropagation; |
|
67
|
|
|
$this->setFormatter($formatter); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __invoke(LogEntryInterface $logEntry) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->isHandling($logEntry->getLevel())) { |
|
76
|
|
|
// format message |
|
77
|
|
|
($this->getFormatter())($logEntry); |
|
78
|
|
|
|
|
79
|
|
|
// write method |
|
80
|
|
|
$this->write($logEntry); |
|
81
|
|
|
|
|
82
|
|
|
// stop propagation |
|
83
|
|
|
if ($this->stop) { |
|
84
|
|
|
$logEntry->stopPropagation(true); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Destructor |
|
91
|
|
|
* |
|
92
|
|
|
* @access public |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __destruct() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->close(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Is this handler handling this level of message ? |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $level |
|
103
|
|
|
* @return bool |
|
104
|
|
|
* @access protected |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function isHandling(/*# string */ $level)/*# : bool */ |
|
107
|
|
|
{ |
|
108
|
|
|
if (LogLevel::$levels[$level] >= LogLevel::$levels[$this->level]) { |
|
109
|
|
|
return true; |
|
110
|
|
|
} else { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Write to the device |
|
117
|
|
|
* |
|
118
|
|
|
* @param LogEntryInterface $logEntry |
|
119
|
|
|
* @access protected |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract protected function write(LogEntryInterface $logEntry); |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Close the handler, to be extended by siblings |
|
125
|
|
|
* |
|
126
|
|
|
* @access protected |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function close() |
|
129
|
|
|
{ |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.