Skip to content

Commit 56af1ac

Browse files
author
Peter
committed
update:
1 parent e189b26 commit 56af1ac

3 files changed

Lines changed: 370 additions & 334 deletions

File tree

app/Util/StdoutLogger.php

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,101 +11,16 @@
1111
namespace App\Util;
1212

1313
use Carbon\Carbon;
14-
use Hyperf\Contract\ConfigInterface;
15-
use Hyperf\Contract\StdoutLoggerInterface;
1614
use Psr\Log\LogLevel;
17-
use Symfony\Component\Console\Output\ConsoleOutput;
18-
use Symfony\Component\Console\Output\OutputInterface;
1915

2016
/**
2117
* Default logger for logging server start and requests.
2218
* PSR-3 logger implementation that logs to STDOUT, using a newline after each
2319
* message. Priority is ignored.
2420
*/
25-
final class StdoutLogger implements StdoutLoggerInterface
21+
final class StdoutLogger extends \Hyperf\Framework\Logger\StdoutLogger
2622
{
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
10924
{
11025
$tag = match ($level) {
11126
LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL => 'error',

0 commit comments

Comments
 (0)