Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
FluentdHandler | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
write | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2018 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\oatbox\log\logger\handler; |
23 | |
24 | use Fluent\Logger\FluentLogger; |
25 | use Monolog\Handler\AbstractProcessingHandler; |
26 | use Monolog\Logger; |
27 | |
28 | /** |
29 | * Based on https://github.com/yegortokmakov/monolog-fluentd |
30 | */ |
31 | class FluentdHandler extends AbstractProcessingHandler |
32 | { |
33 | /** |
34 | * @var FluentLogger |
35 | */ |
36 | private $logger; |
37 | |
38 | /** |
39 | * Initialize Handler |
40 | * |
41 | * @param FluentLogger|string $logger |
42 | * @param int $level |
43 | * @param bool $bubble |
44 | * @param array $options |
45 | */ |
46 | public function __construct($logger = null, $level = Logger::DEBUG, $bubble = true, $options = []) |
47 | { |
48 | parent::__construct($level, $bubble); |
49 | |
50 | if (!is_a($logger, FluentLogger::class, true)) { |
51 | throw new \TypeError('Logger must be an instance of FluentLogger type'); |
52 | } |
53 | if (is_string($logger)) { |
54 | $logger = new $logger(...$options); |
55 | } |
56 | $this->logger = $logger; |
57 | } |
58 | |
59 | /** |
60 | * {@inheritDoc} |
61 | */ |
62 | protected function write(array $record) |
63 | { |
64 | if (isset($record['formatted'])) { |
65 | $logRecord = json_decode($record['formatted'], true); |
66 | } |
67 | |
68 | if (empty($logRecord)) { |
69 | $logRecord = $record['context']; |
70 | $logRecord['level'] = Logger::getLevelName($record['level']); |
71 | $logRecord['message'] = $record['message']; |
72 | } |
73 | $this->logger->post($record['channel'], $logRecord); |
74 | } |
75 | } |