|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Monolog package. |
|
|
5 |
* |
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Monolog; |
|
|
13 |
|
|
|
14 |
use Monolog\Handler\HandlerInterface; |
|
|
15 |
use Monolog\Handler\StreamHandler; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Monolog log channel |
|
|
19 |
* |
|
|
20 |
* It contains a stack of Handlers and a stack of Processors, |
|
|
21 |
* and uses them to store records that are added to it. |
|
|
22 |
* |
|
|
23 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
|
|
24 |
*/ |
|
|
25 |
class Logger |
|
|
26 |
{ |
|
|
27 |
/** |
|
|
28 |
* Detailed debug information |
|
|
29 |
*/ |
|
|
30 |
const DEBUG = 100; |
|
|
31 |
|
|
|
32 |
/** |
|
|
33 |
* Interesting events |
|
|
34 |
* |
|
|
35 |
* Examples: User logs in, SQL logs. |
|
|
36 |
*/ |
|
|
37 |
const INFO = 200; |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Exceptional occurences that are not errors |
|
|
41 |
* |
|
|
42 |
* Examples: Use of deprecated APIs, poor use of an API, |
|
|
43 |
* undesirable things that are not necessarily wrong. |
|
|
44 |
*/ |
|
|
45 |
const WARNING = 300; |
|
|
46 |
|
|
|
47 |
/** |
|
|
48 |
* Runtime errors |
|
|
49 |
*/ |
|
|
50 |
const ERROR = 400; |
|
|
51 |
|
|
|
52 |
/** |
|
|
53 |
* Critical conditions |
|
|
54 |
* |
|
|
55 |
* Example: Application component unavailable, unexpected exception. |
|
|
56 |
*/ |
|
|
57 |
const CRITICAL = 500; |
|
|
58 |
|
|
|
59 |
/** |
|
|
60 |
* Action must be taken immediately |
|
|
61 |
* |
|
|
62 |
* Example: Entire website down, database unavailable, etc. |
|
|
63 |
* This should trigger the SMS alerts and wake you up. |
|
|
64 |
*/ |
|
|
65 |
const ALERT = 550; |
|
|
66 |
|
|
|
67 |
protected static $levels = array( |
|
|
68 |
100 => 'DEBUG', |
|
|
69 |
200 => 'INFO', |
|
|
70 |
300 => 'WARNING', |
|
|
71 |
400 => 'ERROR', |
|
|
72 |
500 => 'CRITICAL', |
|
|
73 |
550 => 'ALERT', |
|
|
74 |
); |
|
|
75 |
|
|
|
76 |
protected $name; |
|
|
77 |
|
|
|
78 |
/** |
|
|
79 |
* The handler stack |
|
|
80 |
* |
|
|
81 |
* @var array of Monolog\Handler\HandlerInterface |
|
|
82 |
*/ |
|
|
83 |
protected $handlers = array(); |
|
|
84 |
|
|
|
85 |
protected $processors = array(); |
|
|
86 |
|
|
|
87 |
/** |
|
|
88 |
* @param string $name The logging channel |
|
|
89 |
*/ |
|
|
90 |
public function __construct($name) |
|
|
91 |
{ |
|
|
92 |
$this->name = $name; |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
/** |
|
|
96 |
* @return string |
|
|
97 |
*/ |
|
|
98 |
public function getName() { |
|
|
99 |
return $this->name; |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* Pushes an handler on the stack. |
|
|
104 |
* |
|
|
105 |
* @param HandlerInterface $handler |
|
|
106 |
*/ |
|
|
107 |
public function pushHandler(HandlerInterface $handler) |
|
|
108 |
{ |
|
|
109 |
array_unshift($this->handlers, $handler); |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
/** |
|
|
113 |
* Pops an handler from the stack |
|
|
114 |
* |
|
|
115 |
* @return HandlerInterface |
|
|
116 |
*/ |
|
|
117 |
public function popHandler() |
|
|
118 |
{ |
|
|
119 |
if (!$this->handlers) { |
|
|
120 |
throw new \LogicException('You tried to pop from an empty handler stack.'); |
|
|
121 |
} |
|
|
122 |
return array_shift($this->handlers); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
/** |
|
|
126 |
* Adds a processor in the stack. |
|
|
127 |
* |
|
|
128 |
* @param callable $callback |
|
|
129 |
*/ |
|
|
130 |
public function pushProcessor($callback) |
|
|
131 |
{ |
|
|
132 |
if (!is_callable($callback)) { |
|
|
133 |
throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given'); |
|
|
134 |
} |
|
|
135 |
array_unshift($this->processors, $callback); |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
/** |
|
|
139 |
* Removes the processor on top of the stack and returns it. |
|
|
140 |
* |
|
|
141 |
* @return callable |
|
|
142 |
*/ |
|
|
143 |
public function popProcessor() |
|
|
144 |
{ |
|
|
145 |
if (!$this->processors) { |
|
|
146 |
throw new \LogicException('You tried to pop from an empty processor stack.'); |
|
|
147 |
} |
|
|
148 |
return array_shift($this->processors); |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
/** |
|
|
152 |
* Adds a log record. |
|
|
153 |
* |
|
|
154 |
* @param integer $level The logging level |
|
|
155 |
* @param string $message The log message |
|
|
156 |
* @param array $context The log context |
|
|
157 |
* @return Boolean Whether the record has been processed |
|
|
158 |
*/ |
|
|
159 |
public function addRecord($level, $message, array $context = array()) |
|
|
160 |
{ |
|
|
161 |
if (!$this->handlers) { |
|
|
162 |
$this->pushHandler(new StreamHandler('php://stderr', self::DEBUG)); |
|
|
163 |
} |
|
|
164 |
$record = array( |
|
|
165 |
'message' => (string) $message, |
|
|
166 |
'context' => $context, |
|
|
167 |
'level' => $level, |
|
|
168 |
'level_name' => self::getLevelName($level), |
|
|
169 |
'channel' => $this->name, |
|
|
170 |
'datetime' => new \DateTime(), |
|
|
171 |
'extra' => array(), |
|
|
172 |
); |
|
|
173 |
// check if any message will handle this message |
|
|
174 |
$handlerKey = null; |
|
|
175 |
foreach ($this->handlers as $key => $handler) { |
|
|
176 |
if ($handler->isHandling($record)) { |
|
|
177 |
$handlerKey = $key; |
|
|
178 |
break; |
|
|
179 |
} |
|
|
180 |
} |
|
|
181 |
// none found |
|
|
182 |
if (null === $handlerKey) { |
|
|
183 |
return false; |
|
|
184 |
} |
|
|
185 |
// found at least one, process message and dispatch it |
|
|
186 |
foreach ($this->processors as $processor) { |
|
|
187 |
$record = call_user_func($processor, $record); |
|
|
188 |
} |
|
|
189 |
while (isset($this->handlers[$handlerKey]) && |
|
|
190 |
false === $this->handlers[$handlerKey]->handle($record)) { |
|
|
191 |
$handlerKey++; |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
return true; |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
/** |
|
|
198 |
* Adds a log record at the DEBUG level. |
|
|
199 |
* |
|
|
200 |
* @param string $message The log message |
|
|
201 |
* @param array $context The log context |
|
|
202 |
* @return Boolean Whether the record has been processed |
|
|
203 |
*/ |
|
|
204 |
public function addDebug($message, array $context = array()) |
|
|
205 |
{ |
|
|
206 |
return $this->addRecord(self::DEBUG, $message, $context); |
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
/** |
|
|
210 |
* Adds a log record at the INFO level. |
|
|
211 |
* |
|
|
212 |
* @param string $message The log message |
|
|
213 |
* @param array $context The log context |
|
|
214 |
* @return Boolean Whether the record has been processed |
|
|
215 |
*/ |
|
|
216 |
public function addInfo($message, array $context = array()) |
|
|
217 |
{ |
|
|
218 |
return $this->addRecord(self::INFO, $message, $context); |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
/** |
|
|
222 |
* Adds a log record at the WARNING level. |
|
|
223 |
* |
|
|
224 |
* @param string $message The log message |
|
|
225 |
* @param array $context The log context |
|
|
226 |
* @return Boolean Whether the record has been processed |
|
|
227 |
*/ |
|
|
228 |
public function addWarning($message, array $context = array()) |
|
|
229 |
{ |
|
|
230 |
return $this->addRecord(self::WARNING, $message, $context); |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
/** |
|
|
234 |
* Adds a log record at the ERROR level. |
|
|
235 |
* |
|
|
236 |
* @param string $message The log message |
|
|
237 |
* @param array $context The log context |
|
|
238 |
* @return Boolean Whether the record has been processed |
|
|
239 |
*/ |
|
|
240 |
public function addError($message, array $context = array()) |
|
|
241 |
{ |
|
|
242 |
return $this->addRecord(self::ERROR, $message, $context); |
|
|
243 |
} |
|
|
244 |
|
|
|
245 |
/** |
|
|
246 |
* Adds a log record at the CRITICAL level. |
|
|
247 |
* |
|
|
248 |
* @param string $message The log message |
|
|
249 |
* @param array $context The log context |
|
|
250 |
* @return Boolean Whether the record has been processed |
|
|
251 |
*/ |
|
|
252 |
public function addCritical($message, array $context = array()) |
|
|
253 |
{ |
|
|
254 |
return $this->addRecord(self::CRITICAL, $message, $context); |
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
/** |
|
|
258 |
* Adds a log record at the ALERT level. |
|
|
259 |
* |
|
|
260 |
* @param string $message The log message |
|
|
261 |
* @param array $context The log context |
|
|
262 |
* @return Boolean Whether the record has been processed |
|
|
263 |
*/ |
|
|
264 |
public function addAlert($message, array $context = array()) |
|
|
265 |
{ |
|
|
266 |
return $this->addRecord(self::ALERT, $message, $context); |
|
|
267 |
} |
|
|
268 |
|
|
|
269 |
/** |
|
|
270 |
* Gets the name of the logging level. |
|
|
271 |
* |
|
|
272 |
* @param integer $level |
|
|
273 |
* @return string |
|
|
274 |
*/ |
|
|
275 |
public static function getLevelName($level) |
|
|
276 |
{ |
|
|
277 |
return self::$levels[$level]; |
|
|
278 |
} |
|
|
279 |
|
|
|
280 |
// ZF Logger Compat |
|
|
281 |
|
|
|
282 |
/** |
|
|
283 |
* Adds a log record at the DEBUG level. |
|
|
284 |
* |
|
|
285 |
* This method allows to have an easy ZF compatibility. |
|
|
286 |
* |
|
|
287 |
* @param string $message The log message |
|
|
288 |
* @param array $context The log context |
|
|
289 |
* @return Boolean Whether the record has been processed |
|
|
290 |
*/ |
|
|
291 |
public function debug($message, array $context = array()) |
|
|
292 |
{ |
|
|
293 |
return $this->addRecord(self::DEBUG, $message, $context); |
|
|
294 |
} |
|
|
295 |
|
|
|
296 |
/** |
|
|
297 |
* Adds a log record at the INFO level. |
|
|
298 |
* |
|
|
299 |
* This method allows to have an easy ZF compatibility. |
|
|
300 |
* |
|
|
301 |
* @param string $message The log message |
|
|
302 |
* @param array $context The log context |
|
|
303 |
* @return Boolean Whether the record has been processed |
|
|
304 |
*/ |
|
|
305 |
public function info($message, array $context = array()) |
|
|
306 |
{ |
|
|
307 |
return $this->addRecord(self::INFO, $message, $context); |
|
|
308 |
} |
|
|
309 |
|
|
|
310 |
/** |
|
|
311 |
* Adds a log record at the INFO level. |
|
|
312 |
* |
|
|
313 |
* This method allows to have an easy ZF compatibility. |
|
|
314 |
* |
|
|
315 |
* @param string $message The log message |
|
|
316 |
* @param array $context The log context |
|
|
317 |
* @return Boolean Whether the record has been processed |
|
|
318 |
*/ |
|
|
319 |
public function notice($message, array $context = array()) |
|
|
320 |
{ |
|
|
321 |
return $this->addRecord(self::INFO, $message, $context); |
|
|
322 |
} |
|
|
323 |
|
|
|
324 |
/** |
|
|
325 |
* Adds a log record at the WARNING level. |
|
|
326 |
* |
|
|
327 |
* This method allows to have an easy ZF compatibility. |
|
|
328 |
* |
|
|
329 |
* @param string $message The log message |
|
|
330 |
* @param array $context The log context |
|
|
331 |
* @return Boolean Whether the record has been processed |
|
|
332 |
*/ |
|
|
333 |
public function warn($message, array $context = array()) |
|
|
334 |
{ |
|
|
335 |
return $this->addRecord(self::WARNING, $message, $context); |
|
|
336 |
} |
|
|
337 |
|
|
|
338 |
/** |
|
|
339 |
* Adds a log record at the ERROR level. |
|
|
340 |
* |
|
|
341 |
* This method allows to have an easy ZF compatibility. |
|
|
342 |
* |
|
|
343 |
* @param string $message The log message |
|
|
344 |
* @param array $context The log context |
|
|
345 |
* @return Boolean Whether the record has been processed |
|
|
346 |
*/ |
|
|
347 |
public function err($message, array $context = array()) |
|
|
348 |
{ |
|
|
349 |
return $this->addRecord(self::ERROR, $message, $context); |
|
|
350 |
} |
|
|
351 |
|
|
|
352 |
/** |
|
|
353 |
* Adds a log record at the CRITICAL level. |
|
|
354 |
* |
|
|
355 |
* This method allows to have an easy ZF compatibility. |
|
|
356 |
* |
|
|
357 |
* @param string $message The log message |
|
|
358 |
* @param array $context The log context |
|
|
359 |
* @return Boolean Whether the record has been processed |
|
|
360 |
*/ |
|
|
361 |
public function crit($message, array $context = array()) |
|
|
362 |
{ |
|
|
363 |
return $this->addRecord(self::CRITICAL, $message, $context); |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
/** |
|
|
367 |
* Adds a log record at the ALERT level. |
|
|
368 |
* |
|
|
369 |
* This method allows to have an easy ZF compatibility. |
|
|
370 |
* |
|
|
371 |
* @param string $message The log message |
|
|
372 |
* @param array $context The log context |
|
|
373 |
* @return Boolean Whether the record has been processed |
|
|
374 |
*/ |
|
|
375 |
public function alert($message, array $context = array()) |
|
|
376 |
{ |
|
|
377 |
return $this->addRecord(self::ALERT, $message, $context); |
|
|
378 |
} |
|
|
379 |
|
|
|
380 |
/** |
|
|
381 |
* Adds a log record at the ALERT level. |
|
|
382 |
* |
|
|
383 |
* This method allows to have an easy ZF compatibility. |
|
|
384 |
* |
|
|
385 |
* @param string $message The log message |
|
|
386 |
* @param array $context The log context |
|
|
387 |
* @return Boolean Whether the record has been processed |
|
|
388 |
*/ |
|
|
389 |
public function emerg($message, array $context = array()) |
|
|
390 |
{ |
|
|
391 |
return $this->addRecord(self::ALERT, $message, $context); |
|
|
392 |
} |
|
|
393 |
} |