WyriHaximus /
php-psr-3-context-logger
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace WyriHaximus\PSR3\ContextLogger; |
||
| 6 | |||
| 7 | use Psr\Log\AbstractLogger; |
||
| 8 | use Psr\Log\LoggerInterface; |
||
| 9 | |||
| 10 | final class ContextLogger extends AbstractLogger |
||
| 11 | { |
||
| 12 | private readonly string $prefix; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param array<mixed> $context |
||
| 16 | * |
||
| 17 | * @phpstan-ignore-next-line |
||
| 18 | */ |
||
| 19 | public function __construct(private readonly LoggerInterface $logger, private readonly array $context, string $prefix = '') |
||
| 20 | { |
||
| 21 | if ($prefix !== '') { |
||
| 22 | $prefix = '[' . $prefix . '] '; |
||
| 23 | } |
||
| 24 | |||
| 25 | $this->prefix = $prefix; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @inheritDoc |
||
| 30 | 16 | * @phpstan-ignore-next-line |
|
| 31 | */ |
||
| 32 | 16 | public function log($level, $message, array $context = []): void |
|
| 33 | 16 | { |
|
| 34 | /** @phpstan-ignore psr3.interpolated */ |
||
| 35 | 16 | $this->logger->log($level, $this->prefix . $message, $this->context + $context); |
|
| 36 | 15 | } |
|
| 37 | } |
||
| 38 |