|
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\Handler; |
|
|
13 |
|
|
|
14 |
use Monolog\Logger; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* Buffers all records until a certain level is reached |
|
|
18 |
* |
|
|
19 |
* The advantage of this approach is that you don't get any clutter in your log files. |
|
|
20 |
* Only requests which actually trigger an error (or whatever your actionLevel is) will be |
|
|
21 |
* in the logs, but they will contain all records, not only those above the level threshold. |
|
|
22 |
* |
|
|
23 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
|
|
24 |
*/ |
|
|
25 |
class FingersCrossedHandler extends AbstractHandler |
|
|
26 |
{ |
|
|
27 |
protected $handler; |
|
|
28 |
protected $actionLevel; |
|
|
29 |
protected $buffering = true; |
|
|
30 |
protected $bufferSize; |
|
|
31 |
protected $buffer = array(); |
|
|
32 |
protected $stopBuffering; |
|
|
33 |
|
|
|
34 |
/** |
|
|
35 |
* @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler). |
|
|
36 |
* @param int $actionLevel The minimum logging level at which this handler will be triggered |
|
|
37 |
* @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. |
|
|
38 |
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
|
|
39 |
* @param Boolean $stopBuffering Whether the handler should stop buffering after being triggered (default true) |
|
|
40 |
*/ |
|
|
41 |
public function __construct($handler, $actionLevel = Logger::WARNING, $bufferSize = 0, $bubble = true, $stopBuffering = true) |
|
|
42 |
{ |
|
|
43 |
$this->handler = $handler; |
|
|
44 |
$this->actionLevel = $actionLevel; |
|
|
45 |
$this->bufferSize = $bufferSize; |
|
|
46 |
$this->bubble = $bubble; |
|
|
47 |
$this->stopBuffering = $stopBuffering; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* {@inheritdoc} |
|
|
52 |
*/ |
|
|
53 |
public function isHandling(array $record) |
|
|
54 |
{ |
|
|
55 |
return true; |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
/** |
|
|
59 |
* {@inheritdoc} |
|
|
60 |
*/ |
|
|
61 |
public function handle(array $record) |
|
|
62 |
{ |
|
|
63 |
if ($this->buffering) { |
|
|
64 |
$this->buffer[] = $record; |
|
|
65 |
if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { |
|
|
66 |
array_shift($this->buffer); |
|
|
67 |
} |
|
|
68 |
if ($record['level'] >= $this->actionLevel) { |
|
|
69 |
if ($this->stopBuffering) { |
|
|
70 |
$this->buffering = false; |
|
|
71 |
} |
|
|
72 |
if (!$this->handler instanceof HandlerInterface) { |
|
|
73 |
$this->handler = call_user_func($this->handler, $record, $this); |
|
|
74 |
} |
|
|
75 |
if (!$this->handler instanceof HandlerInterface) { |
|
|
76 |
throw new \RuntimeException("The factory callback should return a HandlerInterface"); |
|
|
77 |
} |
|
|
78 |
$this->handler->handleBatch($this->buffer); |
|
|
79 |
$this->buffer = array(); |
|
|
80 |
} |
|
|
81 |
} else { |
|
|
82 |
$this->handler->handle($record); |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
return false === $this->bubble; |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
/** |
|
|
89 |
* Resets the state of the handler. Stops forwarding records to the wrapped handler. |
|
|
90 |
*/ |
|
|
91 |
public function reset() |
|
|
92 |
{ |
|
|
93 |
$this->buffering = true; |
|
|
94 |
} |
|
|
95 |
} |