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

Skip to content

Commit 18c845f

Browse files
committed
update: 完善日志格式处理
1 parent 9cf0c9f commit 18c845f

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ thinkphp framework 内置 socket-log 增强替代品
2222
'allow_client_ids' => ['my_develop'],
2323
// client_id 发送方法: path, query, header,推荐使用(query, header)兼容性更好
2424
'client_id_send_method' => 'path',
25-
// 日志处理
25+
// 日志处理(暂无作用)
2626
'processor' => null,
27-
// 关闭通道日志写入
27+
// 关闭通道日志写入(暂无作用)
2828
'close' => false,
29-
// 日志输出格式化
30-
'format' => '[%s][%s] %s',
31-
// 是否实时写入
29+
// 使用分组输出模式
30+
'show_group' => true,
31+
// 日志输出格式化,参数:{date}、{level}、{pid}、{message}
32+
'log_format' => '', // [{date}][{level}] {message}
33+
// 时间格式,配置 log_format 后才有效
34+
'time_format' => \DATE_RFC3339,
35+
// 是否实时写入(暂无作用)
3236
'realtime_write' => false,
3337
// 默认展开节点
3438
'expand_level' => ['debug'],

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"php": "^7.4 | ^8",
1212
"ext-curl": "*",
1313
"ext-json": "*",
14+
"symfony/polyfill-php80": "*",
1415
"topthink/framework": "^6.0 | ^8.0"
1516
},
1617
"require-dev": {

think/SocketV2.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
class SocketV2 implements LogHandlerInterface
1515
{
1616
protected array $config = [
17-
// 使用分组输出模式
18-
'show_group' => true,
19-
// 日志输出格式化
20-
'log_format' => '[{date}][{level}] {message}',
21-
// 时间格式
22-
'time_format' => \DATE_RFC3339,
2317
// socket 服务器连接地址
2418
'uri' => 'http://localhost:1116',
2519
// 是否显示加载的文件列表
@@ -36,6 +30,12 @@ class SocketV2 implements LogHandlerInterface
3630
'expand_level' => ['debug'],
3731
// 日志头渲染回调
3832
'format_head' => null,
33+
// 使用分组输出模式
34+
'show_group' => true,
35+
// 日志输出格式化,参数:{date}、{level}、{pid}、{message}
36+
'log_format' => '', // [{date}][{level}] {message}
37+
// 时间格式
38+
'time_format' => \DATE_RFC3339,
3939
// curl opt
4040
'curl_opts' => [
4141
CURLOPT_CONNECTTIMEOUT => 1,
@@ -61,17 +61,18 @@ class SocketV2 implements LogHandlerInterface
6161
'big' => 'font-size:20px;color:red;',
6262
];
6363
protected array $css2 = [
64-
LogLevel::DEBUG => 'background: rgba(100, 160, 255, 0.15); color: #2b6cb0; padding: 2px 6px; border-radius: 3px;',
65-
LogLevel::INFO => 'background: rgba(100, 200, 150, 0.15); color: #2a7d4f; padding: 2px 6px;',
66-
LogLevel::WARNING => 'background: rgba(255, 193, 7, 0.15); color: #b76e00; padding: 2px 6px;',
67-
LogLevel::ERROR => 'background: rgba(255, 80, 80, 0.15); color: #c62828; padding: 2px 6px;',
68-
LogLevel::EMERGENCY => 'background: rgba(150, 0, 50, 0.15); color: #6d001a; padding: 2px 6px;',
69-
LogLevel::ALERT => 'background: rgba(255, 80, 80, 0.15); color: #c62828; padding: 2px 6px;',
70-
LogLevel::CRITICAL => 'background: rgba(180, 0, 100, 0.15); color: #6a004d; padding: 2px 6px;',
71-
LogLevel::NOTICE => 'background: rgba(0, 150, 200, 0.15); color: #005f7c; padding: 2px 6px;',
64+
LogLevel::DEBUG => 'background: rgba(173, 216, 230, 0.15); color: #1e4d8c; padding: 2px 6px; padding: 2px 6px; border-radius: 3px;',
65+
LogLevel::INFO => 'background: rgba(144, 238, 144, 0.15); color: #206a3d; padding: 2px 6px;',
66+
LogLevel::WARNING => 'background: rgba(255, 228, 181, 0.15); color: #9c6e00; padding: 2px 6px;',
67+
LogLevel::ERROR => 'background: rgba(255, 192, 203, 0.15); color: #a31212; padding: 2px 6px;',
68+
LogLevel::EMERGENCY => 'background: rgba(255, 182, 193, 0.15); color: #6a0016; padding: 2px 6px;',
69+
LogLevel::ALERT => 'background: rgba(255, 218, 185, 0.15); color: #cc5500; padding: 2px 6px;',
70+
LogLevel::CRITICAL => 'background: rgba(230, 190, 255, 0.15); color: #6a006a; padding: 2px 6px;',
71+
LogLevel::NOTICE => 'background: rgba(173, 216, 230, 0.15); color: #004a77; padding: 2px 6px;',
7272
// 自定义
7373
'route' => 'info',
74-
'request' => 'info',
74+
// 'request' => 'info',
75+
'sql' => 'warning',
7576
];
7677

7778
protected array $allowForceClientIds = []; //配置强制推送且被授权的client_id
@@ -184,6 +185,8 @@ public function save(array $log = []): bool
184185
];
185186
}
186187

188+
$format = trim($this->config['log_format'] ?? '');
189+
187190
if ($this->config['show_group'] ?? true) {
188191
$expandLevel = array_flip($this->config['expand_level']);
189192

@@ -197,6 +200,7 @@ public function save(array $log = []): bool
197200
if (!is_string($msg)) {
198201
$msg = var_export($msg, true);
199202
}
203+
$msg = $format ? $this->formatMessage($format, $type, $msg) : "[{$type}] {$messages}";
200204
$trace[] = [
201205
'type' => 'log',
202206
'msg' => $msg,
@@ -210,8 +214,6 @@ public function save(array $log = []): bool
210214
];
211215
}
212216
} else {
213-
$format = trim($this->config['log_format'] ?? '');
214-
215217
$trace[] = [
216218
'type' => 'group',
217219
'msg' => 'logs',
@@ -288,12 +290,18 @@ public function save(array $log = []): bool
288290

289291
protected function formatMessage(string $format, string $level, string $messages, ?array $context = null): string
290292
{
291-
/** @var \DateTimeInterface $date */
293+
if (!str_contains($format, '{')) {
294+
return "[{$level}] {$messages}";
295+
}
296+
/** @var \DateTimeInterface|null $date */
292297
$date = $context["\0_t"] ?? null;
298+
/** @var int|null $index */
299+
$index = $context["\0_i"] ?? null;
293300

294301
$replace = [
295302
'{date}' => $date ? $date->format($this->config['time_format']) : '',
296303
'{level}' => $level,
304+
'{index}' => $index,
297305
'{pid}' => getmypid(),
298306
'{message}' => $messages,
299307
];

0 commit comments

Comments
 (0)