1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Console; |
4: | |
5: | use Psr\Log\InvalidArgumentException; |
6: | use Psr\Log\LoggerInterface; |
7: | use Psr\Log\LogLevel; |
8: | use Salient\Contract\Console\ConsoleInterface as Console; |
9: | use Salient\Utility\Format; |
10: | use Throwable; |
11: | |
12: | |
13: | |
14: | |
15: | final class ConsoleLogger implements LoggerInterface |
16: | { |
17: | private const LOG_LEVEL_MAP = [ |
18: | LogLevel::EMERGENCY => Console::LEVEL_EMERGENCY, |
19: | LogLevel::ALERT => Console::LEVEL_ALERT, |
20: | LogLevel::CRITICAL => Console::LEVEL_CRITICAL, |
21: | LogLevel::ERROR => Console::LEVEL_ERROR, |
22: | LogLevel::WARNING => Console::LEVEL_WARNING, |
23: | LogLevel::NOTICE => Console::LEVEL_NOTICE, |
24: | LogLevel::INFO => Console::LEVEL_INFO, |
25: | LogLevel::DEBUG => Console::LEVEL_DEBUG, |
26: | ]; |
27: | |
28: | private Console $Console; |
29: | |
30: | public function __construct(Console $console) |
31: | { |
32: | $this->Console = $console; |
33: | } |
34: | |
35: | |
36: | |
37: | |
38: | public function emergency($message, array $context = []) |
39: | { |
40: | $this->log(LogLevel::EMERGENCY, $message, $context); |
41: | } |
42: | |
43: | |
44: | |
45: | |
46: | public function alert($message, array $context = []) |
47: | { |
48: | $this->log(LogLevel::ALERT, $message, $context); |
49: | } |
50: | |
51: | |
52: | |
53: | |
54: | public function critical($message, array $context = []) |
55: | { |
56: | $this->log(LogLevel::CRITICAL, $message, $context); |
57: | } |
58: | |
59: | |
60: | |
61: | |
62: | public function error($message, array $context = []) |
63: | { |
64: | $this->log(LogLevel::ERROR, $message, $context); |
65: | } |
66: | |
67: | |
68: | |
69: | |
70: | public function warning($message, array $context = []) |
71: | { |
72: | $this->log(LogLevel::WARNING, $message, $context); |
73: | } |
74: | |
75: | |
76: | |
77: | |
78: | public function notice($message, array $context = []) |
79: | { |
80: | $this->log(LogLevel::NOTICE, $message, $context); |
81: | } |
82: | |
83: | |
84: | |
85: | |
86: | public function info($message, array $context = []) |
87: | { |
88: | $this->log(LogLevel::INFO, $message, $context); |
89: | } |
90: | |
91: | |
92: | |
93: | |
94: | public function debug($message, array $context = []) |
95: | { |
96: | $this->log(LogLevel::DEBUG, $message, $context); |
97: | } |
98: | |
99: | |
100: | |
101: | |
102: | public function log($level, $message, array $context = []) |
103: | { |
104: | if (!isset(self::LOG_LEVEL_MAP[$level])) { |
105: | throw new InvalidArgumentException('Invalid log level'); |
106: | } |
107: | |
108: | if ($context) { |
109: | foreach ($context as $key => $value) { |
110: | $replace['{' . $key . '}'] = Format::value($value); |
111: | } |
112: | $message = strtr($message, $replace); |
113: | |
114: | if ( |
115: | isset($context['exception']) |
116: | && $context['exception'] instanceof Throwable |
117: | ) { |
118: | $exception = $context['exception']; |
119: | } |
120: | } |
121: | |
122: | $this->Console->message( |
123: | (string) $message, |
124: | null, |
125: | self::LOG_LEVEL_MAP[$level], |
126: | Console::TYPE_STANDARD, |
127: | $exception ?? null, |
128: | ); |
129: | } |
130: | } |
131: | |