Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a636c6d

Browse files
committed
update: 兼容 thinkphp 8.1 新版日志改动
1 parent ad7d0d4 commit a636c6d

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

think/SocketV2.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Log\LogLevel;
1010
use think\App;
1111
use think\contract\LogHandlerInterface;
12+
use think\event\LogRecord;
1213
use Zxin\SocketLog\SocketClient;
1314

1415
class SocketV2 implements LogHandlerInterface
@@ -123,27 +124,30 @@ public function __construct(App $app, array $config = [])
123124
}
124125
}
125126

126-
protected function logReader(array $log, bool $group): \Generator
127+
protected function logReader(array $logs, bool $group): \Generator
127128
{
128129
// 是否启用兼容模式的备用判断
129-
$newImplement = $this->newImplement ?? array_is_list($log);
130+
$newImplement = $this->newImplement ?? array_is_list($logs);
130131

131132
if ($newImplement) {
132133
if ($group) {
133134
$group = [];
134-
foreach ($log as [$type, $msg]) {
135-
$group[$type][] = $msg;
135+
foreach ($logs as $log) {
136+
/** @var LogRecord $log */
137+
$group[$log->type][] = $log;
136138
}
137139
yield from $group;
138140
} else {
139-
yield from $log;
141+
yield from $logs;
140142
}
141143
} else {
142144
if ($group) {
143-
yield from $log;
145+
foreach ($logs as $type => $msgLines) {
146+
yield $type => array_map(fn ($msgLine) => new LogRecord($type, $msgLine), $msgLines);
147+
}
144148
} else {
145-
foreach ($log as $type => $msg) {
146-
yield [$type, $msg];
149+
foreach ($logs as $type => $msg) {
150+
yield new LogRecord($type, $msg);
147151
}
148152
}
149153
}
@@ -196,9 +200,9 @@ public function save(array $log = []): bool
196200
'msg' => '[ ' . $type . ' ]',
197201
'css' => $this->css[$type] ?? '',
198202
];
199-
foreach ($messages as $msg) {
200-
$msg = $this->normalizeMessage($msg);
201-
$msg = $format ? $this->formatMessage($format, $type, $msg) : "[{$type}] {$msg}";
203+
foreach ($messages as $logRecord) {
204+
/** @var LogRecord $logRecord */
205+
$msg = $format ? $this->formatMessage($format, $logRecord) : "[{$type}] {$this->normalizeMessage($logRecord->message)}";
202206
$trace[] = [
203207
'type' => 'log',
204208
'msg' => $msg,
@@ -218,14 +222,14 @@ public function save(array $log = []): bool
218222
'css' => '',
219223
];
220224
foreach ($this->logReader($log, false) as $item) {
221-
[$type, $messages] = $item;
222-
$ctx = $item[2] ?? null;
223-
$messages = $this->normalizeMessage($messages);
225+
/** @var LogRecord $item */
226+
$type = $item->type;
224227
$css = $this->css2[$type] ?? '';
225228
if (in_array($css, self::LogLevelSet, true)) {
226229
$css = $this->css2[$css] ?? '';
227230
}
228-
$msg = $format ? $this->formatMessage($format, $type, $messages, $ctx) : "[{$type}] {$messages}";
231+
232+
$msg = $format ? $this->formatMessage($format, $item) : "[{$type}] {$this->normalizeMessage($item->message)}";
229233
$trace[] = [
230234
'type' => 'log',
231235
'msg' => $msg,
@@ -301,20 +305,20 @@ protected function normalizeMessage($message): string
301305
return $message;
302306
}
303307

304-
protected function formatMessage(string $format, string $level, string $messages, ?array $context = null): string
308+
protected function formatMessage(string $format, LogRecord $logRecord): string
305309
{
310+
$level = $logRecord->type;
311+
$messages = $this->normalizeMessage($logRecord->message);
312+
306313
if (!str_contains($format, '{')) {
307314
return "[{$level}] {$messages}";
308315
}
309316
/** @var \DateTimeInterface|null $date */
310-
$date = $context["\0_t"] ?? null;
311-
/** @var int|null $index */
312-
$index = $context["\0_i"] ?? null;
317+
$date = $logRecord->time ?? null;
313318

314319
$replace = [
315320
'{date}' => $date ? $date->format($this->config['time_format']) : '',
316321
'{level}' => $level,
317-
'{index}' => $index,
318322
'{pid}' => getmypid(),
319323
'{message}' => $messages,
320324
];

0 commit comments

Comments
 (0)