1: <?php declare(strict_types=1);
2:
3: namespace Salient\Console\Target;
4:
5: use Psr\Log\LoggerAwareInterface;
6: use Psr\Log\LoggerInterface;
7: use Psr\Log\LogLevel;
8: use Salient\Console\Concept\ConsoleTarget;
9: use Salient\Contract\Core\MessageLevel as Level;
10:
11: /**
12: * Writes console output to a PSR-3 logger
13: */
14: final class LoggerTarget extends ConsoleTarget implements LoggerAwareInterface
15: {
16: /**
17: * @var array<Level::*,LogLevel::*>
18: */
19: private const LOG_LEVEL_MAP = [
20: Level::EMERGENCY => LogLevel::EMERGENCY,
21: Level::ALERT => LogLevel::ALERT,
22: Level::CRITICAL => LogLevel::CRITICAL,
23: Level::ERROR => LogLevel::ERROR,
24: Level::WARNING => LogLevel::WARNING,
25: Level::NOTICE => LogLevel::NOTICE,
26: Level::INFO => LogLevel::INFO,
27: Level::DEBUG => LogLevel::DEBUG,
28: ];
29:
30: private LoggerInterface $Logger;
31:
32: public function __construct(LoggerInterface $logger)
33: {
34: $this->setLogger($logger);
35: }
36:
37: /**
38: * @inheritDoc
39: */
40: public function setLogger(LoggerInterface $logger)
41: {
42: $this->Logger = $logger;
43: }
44:
45: /**
46: * @inheritDoc
47: */
48: public function write($level, string $message, array $context = []): void
49: {
50: $this->Logger->log(self::LOG_LEVEL_MAP[$level], $message, $context);
51: }
52: }
53: