|
11 | 11 | namespace App\Util; |
12 | 12 |
|
13 | 13 | use Carbon\Carbon; |
14 | | -use Hyperf\Contract\ConfigInterface; |
15 | | -use Hyperf\Contract\StdoutLoggerInterface; |
16 | 14 | use Psr\Log\LogLevel; |
17 | | -use Symfony\Component\Console\Output\ConsoleOutput; |
18 | | -use Symfony\Component\Console\Output\OutputInterface; |
19 | 15 |
|
20 | 16 | /** |
21 | 17 | * Default logger for logging server start and requests. |
22 | 18 | * PSR-3 logger implementation that logs to STDOUT, using a newline after each |
23 | 19 | * message. Priority is ignored. |
24 | 20 | */ |
25 | | -final class StdoutLogger implements StdoutLoggerInterface |
| 21 | +final class StdoutLogger extends \Hyperf\Framework\Logger\StdoutLogger |
26 | 22 | { |
27 | | - private OutputInterface $output; |
28 | | - |
29 | | - /** |
30 | | - * @var array<string> |
31 | | - */ |
32 | | - private array $tags = [ |
33 | | - 'context', |
34 | | - 'extra', |
35 | | - ]; |
36 | | - |
37 | | - public function __construct(private readonly ConfigInterface $config, ?OutputInterface $output = null) |
38 | | - { |
39 | | - $this->output = $output ?? new ConsoleOutput(); |
40 | | - } |
41 | | - |
42 | | - public function emergency($message, array $context = []): void |
43 | | - { |
44 | | - $this->log(LogLevel::EMERGENCY, $message, $context); |
45 | | - } |
46 | | - |
47 | | - public function alert($message, array $context = []): void |
48 | | - { |
49 | | - $this->log(LogLevel::ALERT, $message, $context); |
50 | | - } |
51 | | - |
52 | | - public function critical($message, array $context = []): void |
53 | | - { |
54 | | - $this->log(LogLevel::CRITICAL, $message, $context); |
55 | | - } |
56 | | - |
57 | | - public function error($message, array $context = []): void |
58 | | - { |
59 | | - $this->log(LogLevel::ERROR, $message, $context); |
60 | | - } |
61 | | - |
62 | | - public function warning($message, array $context = []): void |
63 | | - { |
64 | | - $this->log(LogLevel::WARNING, $message, $context); |
65 | | - } |
66 | | - |
67 | | - public function notice($message, array $context = []): void |
68 | | - { |
69 | | - $this->log(LogLevel::NOTICE, $message, $context); |
70 | | - } |
71 | | - |
72 | | - public function info($message, array $context = []): void |
73 | | - { |
74 | | - $this->log(LogLevel::INFO, $message, $context); |
75 | | - } |
76 | | - |
77 | | - public function debug($message, array $context = []): void |
78 | | - { |
79 | | - $this->log(LogLevel::DEBUG, $message, $context); |
80 | | - } |
81 | | - |
82 | | - public function log($level, $message, array $context = []): void |
83 | | - { |
84 | | - $config = $this->config->get(StdoutLoggerInterface::class, ['log_level' => []]); |
85 | | - if (! in_array($level, $config['log_level'], true)) { |
86 | | - return; |
87 | | - } |
88 | | - $keys = array_keys($context); |
89 | | - $tags = []; |
90 | | - foreach ($keys as $k => $key) { |
91 | | - if (in_array($key, $this->tags, true)) { |
92 | | - $tags[$key] = $context[$key]; |
93 | | - unset($keys[$k]); |
94 | | - } |
95 | | - } |
96 | | - $search = array_map(static function ($key) { |
97 | | - return \sprintf('{%s}', $key); |
98 | | - }, $keys); |
99 | | - |
100 | | - $message = \str_replace($search, $context, $this->getMessage((string) $message, $level, $tags)); |
101 | | - |
102 | | - try { |
103 | | - $this->output->writeln($message . ' ' . json_encode($context, JSON_THROW_ON_ERROR)); |
104 | | - } catch (\JsonException $e) { |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - private function getMessage(string $message, string $level = LogLevel::INFO, array $tags = []): string |
| 23 | + protected function getMessage(string $message, string $level = LogLevel::INFO, array $tags = []): string |
109 | 24 | { |
110 | 25 | $tag = match ($level) { |
111 | 26 | LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL => 'error', |
|
0 commit comments