1: <?php declare(strict_types=1);
2:
3: namespace Salient\Console\Target;
4:
5: use Psr\Log\LoggerAwareInterface as PsrLoggerAwareInterface;
6: use Psr\Log\LoggerInterface as PsrLoggerInterface;
7: use Psr\Log\LogLevel as PsrLogLevel;
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 PsrLoggerAwareInterface
16: {
17: /**
18: * @var array<Console::LEVEL_*,PsrLogLevel::*>
19: */
20: private const LOG_LEVEL_MAP = [
21: Console::LEVEL_EMERGENCY => PsrLogLevel::EMERGENCY,
22: Console::LEVEL_ALERT => PsrLogLevel::ALERT,
23: Console::LEVEL_CRITICAL => PsrLogLevel::CRITICAL,
24: Console::LEVEL_ERROR => PsrLogLevel::ERROR,
25: Console::LEVEL_WARNING => PsrLogLevel::WARNING,
26: Console::LEVEL_NOTICE => PsrLogLevel::NOTICE,
27: Console::LEVEL_INFO => PsrLogLevel::INFO,
28: Console::LEVEL_DEBUG => PsrLogLevel::DEBUG,
29: ];
30:
31: private PsrLoggerInterface $Logger;
32:
33: /**
34: * @api
35: */
36: public function __construct(PsrLoggerInterface $logger)
37: {
38: $this->setLogger($logger);
39: }
40:
41: /**
42: * @inheritDoc
43: */
44: public function setLogger(PsrLoggerInterface $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: