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
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HandlerAbstract |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Logger |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see ObjectAbstract |
29
|
|
|
* @see HandlerInterface |
30
|
|
|
* @version 2.0.0 |
31
|
|
|
* @since 2.0.0 added |
32
|
|
|
*/ |
33
|
|
|
abstract class HandlerAbstract extends ObjectAbstract implements HandlerInterface |
34
|
|
|
{ |
35
|
|
|
use FormatterAwareTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $level; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Stop log propagation |
45
|
|
|
* |
46
|
|
|
* @var bool |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
protected $stop; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Created with level handling |
53
|
|
|
* |
54
|
|
|
* @param string $logLevel |
|
|
|
|
55
|
|
|
* @param FormatterInterface $formatter |
56
|
|
|
* @param bool $stopPropagation |
57
|
|
|
* @access public |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
/*# string */ $level = LogLevel::WARNING, |
61
|
|
|
FormatterInterface $formatter = null, |
62
|
|
|
/*# bool */ $stopPropagation = false |
63
|
|
|
) { |
64
|
|
|
$this->level = $level; |
65
|
|
|
$this->stop = (bool) $stopPropagation; |
66
|
|
|
$this->setFormatter($formatter); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function __invoke(LogEntryInterface $logEntry) |
73
|
|
|
{ |
74
|
|
|
if ($this->isHandling($logEntry)) { |
75
|
|
|
// format message |
76
|
|
|
($this->getFormatter())($logEntry); |
77
|
|
|
|
78
|
|
|
// write method |
79
|
|
|
$this->write($logEntry); |
80
|
|
|
|
81
|
|
|
// stop propagation |
82
|
|
|
if ($this->stop) { |
83
|
|
|
$logEntry->stopPropagation(true); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Destructor |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
*/ |
93
|
|
|
public function __destruct() |
94
|
|
|
{ |
95
|
|
|
$this->close(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Is this handler handling this log ? |
100
|
|
|
* |
101
|
|
|
* @param LogEntryInterface $logEntry |
102
|
|
|
* @return bool |
103
|
|
|
* @access protected |
104
|
|
|
*/ |
105
|
|
|
protected function isHandling(LogEntryInterface $logEntry)/*# : bool */ |
106
|
|
|
{ |
107
|
|
|
if ($this->isHandlingLevel($logEntry->getLevel()) && |
108
|
|
|
$this->isHandlingOther($logEntry) |
109
|
|
|
) { |
110
|
|
|
return true; |
111
|
|
|
} else { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Is this handler handling this log level ? |
118
|
|
|
* |
119
|
|
|
* @param string $level |
120
|
|
|
* @return bool |
121
|
|
|
* @access protected |
122
|
|
|
*/ |
123
|
|
|
protected function isHandlingLevel(/*# string */ $level)/*# : bool */ |
124
|
|
|
{ |
125
|
|
|
if (LogLevel::$levels[$level] >= LogLevel::$levels[$this->level]) { |
126
|
|
|
return true; |
127
|
|
|
} else { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* To be overriden by siblings |
134
|
|
|
* |
135
|
|
|
* @param LogEntryInterface $logEntry |
136
|
|
|
* @return bool |
137
|
|
|
* @access protected |
138
|
|
|
*/ |
139
|
|
|
protected function isHandlingOther(LogEntryInterface $logEntry)/*# : bool */ |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Write to the device |
146
|
|
|
* |
147
|
|
|
* @param LogEntryInterface $logEntry |
148
|
|
|
* @access protected |
149
|
|
|
*/ |
150
|
|
|
abstract protected function write(LogEntryInterface $logEntry); |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Close the handler, to be extended by siblings |
154
|
|
|
* |
155
|
|
|
* @access protected |
156
|
|
|
*/ |
157
|
|
|
protected function close() |
158
|
|
|
{ |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get EOL char base on platform windows or Unix |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
* @access protected |
166
|
|
|
*/ |
167
|
|
|
protected function getEol()/*# : string */ |
168
|
|
|
{ |
169
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
170
|
|
|
return "\r\n"; |
171
|
|
|
|
172
|
|
|
} else { |
173
|
|
|
return "\n"; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Test to see if in CLI mode |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
* @access protected |
182
|
|
|
*/ |
183
|
|
|
protected function isCliMode()/*# : bool */ |
184
|
|
|
{ |
185
|
|
|
return 'cli' === php_sapi_name(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.