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: | 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: | |
31: | |
32: | |
33: | public function __construct(Console $console) |
34: | { |
35: | $this->Console = $console; |
36: | } |
37: | |
38: | |
39: | |
40: | |
41: | public function emergency($message, array $context = []) |
42: | { |
43: | $this->log(LogLevel::EMERGENCY, $message, $context); |
44: | } |
45: | |
46: | |
47: | |
48: | |
49: | public function alert($message, array $context = []) |
50: | { |
51: | $this->log(LogLevel::ALERT, $message, $context); |
52: | } |
53: | |
54: | |
55: | |
56: | |
57: | public function critical($message, array $context = []) |
58: | { |
59: | $this->log(LogLevel::CRITICAL, $message, $context); |
60: | } |
61: | |
62: | |
63: | |
64: | |
65: | public function error($message, array $context = []) |
66: | { |
67: | $this->log(LogLevel::ERROR, $message, $context); |
68: | } |
69: | |
70: | |
71: | |
72: | |
73: | public function warning($message, array $context = []) |
74: | { |
75: | $this->log(LogLevel::WARNING, $message, $context); |
76: | } |
77: | |
78: | |
79: | |
80: | |
81: | public function notice($message, array $context = []) |
82: | { |
83: | $this->log(LogLevel::NOTICE, $message, $context); |
84: | } |
85: | |
86: | |
87: | |
88: | |
89: | public function info($message, array $context = []) |
90: | { |
91: | $this->log(LogLevel::INFO, $message, $context); |
92: | } |
93: | |
94: | |
95: | |
96: | |
97: | public function debug($message, array $context = []) |
98: | { |
99: | $msg1 = $this->applyContext((string) $message, $context, $ex); |
100: | $this->Console->debug($msg1, null, $ex, 1); |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | public function log($level, $message, array $context = []) |
107: | { |
108: | if (!isset(self::LOG_LEVEL_MAP[$level])) { |
109: | throw new InvalidArgumentException('Invalid log level'); |
110: | } |
111: | |
112: | $msg1 = $this->applyContext((string) $message, $context, $ex); |
113: | $level = self::LOG_LEVEL_MAP[$level]; |
114: | $this->Console->message($msg1, null, $level, Console::TYPE_STANDARD, $ex); |
115: | } |
116: | |
117: | |
118: | |
119: | |
120: | private function applyContext(string $message, array $context, ?Throwable &$ex): string |
121: | { |
122: | if ($context) { |
123: | foreach ($context as $key => $value) { |
124: | $replace['{' . $key . '}'] = Format::value($value); |
125: | } |
126: | $message = strtr($message, $replace); |
127: | |
128: | if ( |
129: | isset($context['exception']) |
130: | && $context['exception'] instanceof Throwable |
131: | ) { |
132: | $ex = $context['exception']; |
133: | } |
134: | } |
135: | |
136: | return $message; |
137: | } |
138: | } |
139: | |