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\Contract\Console\ConsoleInterface as Console;
9:
10: /**
11: * Writes console output to a PSR-3 logger
12: *
13: * @api
14: */
15: class LoggerTarget extends AbstractTarget implements LoggerAwareInterface
16: {
17: /**
18: * @var array<Console::LEVEL_*,LogLevel::*>
19: */
20: private const LOG_LEVEL_MAP = [
21: Console::LEVEL_EMERGENCY => LogLevel::EMERGENCY,
22: Console::LEVEL_ALERT => LogLevel::ALERT,
23: Console::LEVEL_CRITICAL => LogLevel::CRITICAL,
24: Console::LEVEL_ERROR => LogLevel::ERROR,
25: Console::LEVEL_WARNING => LogLevel::WARNING,
26: Console::LEVEL_NOTICE => LogLevel::NOTICE,
27: Console::LEVEL_INFO => LogLevel::INFO,
28: Console::LEVEL_DEBUG => LogLevel::DEBUG,
29: ];
30:
31: private LoggerInterface $Logger;
32:
33: /**
34: * @api
35: */
36: public function __construct(LoggerInterface $logger)
37: {
38: $this->setLogger($logger);
39: }
40:
41: /**
42: * @inheritDoc
43: */
44: public function setLogger(LoggerInterface $logger): void
45: {
46: $this->Logger = $logger;
47: }
48:
49: /**
50: * @inheritDoc
51: */
52: public function write(int $level, string $message, array $context = []): void
53: {
54: $this->Logger->log(self::LOG_LEVEL_MAP[$level], $message, $context);
55: }
56: }
57: