|
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 closing the handler and then pass them as batch. |
|
|
18 |
* |
|
|
19 |
* This is useful for a MailHandler to send only one mail per request instead of |
|
|
20 |
* sending one per log message. |
|
|
21 |
* |
|
|
22 |
* @author Christophe Coevoet <stof@notk.org> |
|
|
23 |
*/ |
|
|
24 |
class BufferHandler extends AbstractHandler |
|
|
25 |
{ |
|
|
26 |
protected $handler; |
|
|
27 |
protected $bufferSize; |
|
|
28 |
protected $buffer = array(); |
|
|
29 |
|
|
|
30 |
/** |
|
|
31 |
* @param HandlerInterface $handler Handler. |
|
|
32 |
* @param integer $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. |
|
|
33 |
* @param integer $level The minimum logging level at which this handler will be triggered |
|
|
34 |
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not |
|
|
35 |
*/ |
|
|
36 |
public function __construct(HandlerInterface $handler, $bufferSize = 0, $level = Logger::DEBUG, $bubble = true) |
|
|
37 |
{ |
|
|
38 |
parent::__construct($level, $bubble); |
|
|
39 |
$this->handler = $handler; |
|
|
40 |
$this->bufferSize = $bufferSize; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
/** |
|
|
44 |
* {@inheritdoc} |
|
|
45 |
*/ |
|
|
46 |
public function handle(array $record) |
|
|
47 |
{ |
|
|
48 |
if ($record['level'] < $this->level) { |
|
|
49 |
return false; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
$this->buffer[] = $record; |
|
|
53 |
if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { |
|
|
54 |
array_shift($this->buffer); |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
return false === $this->bubble; |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
/** |
|
|
61 |
* {@inheritdoc} |
|
|
62 |
*/ |
|
|
63 |
public function close() |
|
|
64 |
{ |
|
|
65 |
$this->handler->handleBatch($this->buffer); |
|
|
66 |
} |
|
|
67 |
} |