Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.47% |
17 / 19 |
|
84.62% |
11 / 13 |
CRAP | |
0.00% |
0 / 1 |
| AdvancedLogger | |
89.47% |
17 / 19 |
|
84.62% |
11 / 13 |
15.26 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addContextExtender | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| emergency | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| alert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| critical | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| warning | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| notice | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| info | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| debug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| log | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| logData | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| extendContext | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 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) 2021 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\log\logger; |
| 24 | |
| 25 | use oat\oatbox\log\logger\extender\ContextExtenderInterface; |
| 26 | use Psr\Log\LoggerInterface; |
| 27 | |
| 28 | class AdvancedLogger implements LoggerInterface |
| 29 | { |
| 30 | public const ACL_SERVICE_ID = self::class . '::ACL'; |
| 31 | |
| 32 | /** @var LoggerInterface */ |
| 33 | private $logger; |
| 34 | |
| 35 | /** @var ContextExtenderInterface[] */ |
| 36 | private $contextExtenders = []; |
| 37 | |
| 38 | public function __construct(LoggerInterface $logger) |
| 39 | { |
| 40 | $this->logger = $logger; |
| 41 | } |
| 42 | |
| 43 | public function addContextExtender(ContextExtenderInterface $contextExtender): self |
| 44 | { |
| 45 | $this->contextExtenders[get_class($contextExtender)] = $contextExtender; |
| 46 | |
| 47 | return $this; |
| 48 | } |
| 49 | |
| 50 | public function emergency($message, array $context = []) |
| 51 | { |
| 52 | $this->logData('emergency', $message, $context); |
| 53 | } |
| 54 | |
| 55 | public function alert($message, array $context = []) |
| 56 | { |
| 57 | $this->logData('alert', $message, $context); |
| 58 | } |
| 59 | |
| 60 | public function critical($message, array $context = []) |
| 61 | { |
| 62 | $this->logData('critical', $message, $context); |
| 63 | } |
| 64 | |
| 65 | public function error($message, array $context = []) |
| 66 | { |
| 67 | $this->logData('error', $message, $context); |
| 68 | } |
| 69 | |
| 70 | public function warning($message, array $context = []) |
| 71 | { |
| 72 | $this->logData('warning', $message, $context); |
| 73 | } |
| 74 | |
| 75 | public function notice($message, array $context = []) |
| 76 | { |
| 77 | $this->logData('notice', $message, $context); |
| 78 | } |
| 79 | |
| 80 | public function info($message, array $context = []) |
| 81 | { |
| 82 | $this->logData('info', $message, $context); |
| 83 | } |
| 84 | |
| 85 | public function debug($message, array $context = []) |
| 86 | { |
| 87 | $this->logData('debug', $message, $context); |
| 88 | } |
| 89 | |
| 90 | public function log($level, $message, array $context = []) |
| 91 | { |
| 92 | $this->logData('log', $message, $context, $level); |
| 93 | } |
| 94 | |
| 95 | private function logData(string $methodName, $message, array $context = [], $level = null): void |
| 96 | { |
| 97 | $context = $this->extendContext($context); |
| 98 | |
| 99 | $level === null |
| 100 | ? $this->logger->{$methodName}($message, $context) |
| 101 | : $this->logger->{$methodName}($level, $message, $context); |
| 102 | } |
| 103 | |
| 104 | private function extendContext(array $context): array |
| 105 | { |
| 106 | foreach ($this->contextExtenders as $contextExtender) { |
| 107 | $context = $contextExtender->extend($context); |
| 108 | } |
| 109 | |
| 110 | return $context; |
| 111 | } |
| 112 | } |