14
14
class SocketV2 implements LogHandlerInterface
15
15
{
16
16
protected array $ config = [
17
- // 使用分组输出模式
18
- 'show_group ' => true ,
19
- // 日志输出格式化
20
- 'log_format ' => '[{date}][{level}] {message} ' ,
21
- // 时间格式
22
- 'time_format ' => \DATE_RFC3339 ,
23
17
// socket 服务器连接地址
24
18
'uri ' => 'http://localhost:1116 ' ,
25
19
// 是否显示加载的文件列表
@@ -36,6 +30,12 @@ class SocketV2 implements LogHandlerInterface
36
30
'expand_level ' => ['debug ' ],
37
31
// 日志头渲染回调
38
32
'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 ,
39
39
// curl opt
40
40
'curl_opts ' => [
41
41
CURLOPT_CONNECTTIMEOUT => 1 ,
@@ -61,17 +61,18 @@ class SocketV2 implements LogHandlerInterface
61
61
'big ' => 'font-size:20px;color:red; ' ,
62
62
];
63
63
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; ' ,
72
72
// 自定义
73
73
'route ' => 'info ' ,
74
- 'request ' => 'info ' ,
74
+ // 'request' => 'info',
75
+ 'sql ' => 'warning ' ,
75
76
];
76
77
77
78
protected array $ allowForceClientIds = []; //配置强制推送且被授权的client_id
@@ -184,6 +185,8 @@ public function save(array $log = []): bool
184
185
];
185
186
}
186
187
188
+ $ format = trim ($ this ->config ['log_format ' ] ?? '' );
189
+
187
190
if ($ this ->config ['show_group ' ] ?? true ) {
188
191
$ expandLevel = array_flip ($ this ->config ['expand_level ' ]);
189
192
@@ -197,6 +200,7 @@ public function save(array $log = []): bool
197
200
if (!is_string ($ msg )) {
198
201
$ msg = var_export ($ msg , true );
199
202
}
203
+ $ msg = $ format ? $ this ->formatMessage ($ format , $ type , $ msg ) : "[ {$ type }] {$ messages }" ;
200
204
$ trace [] = [
201
205
'type ' => 'log ' ,
202
206
'msg ' => $ msg ,
@@ -210,8 +214,6 @@ public function save(array $log = []): bool
210
214
];
211
215
}
212
216
} else {
213
- $ format = trim ($ this ->config ['log_format ' ] ?? '' );
214
-
215
217
$ trace [] = [
216
218
'type ' => 'group ' ,
217
219
'msg ' => 'logs ' ,
@@ -288,12 +290,18 @@ public function save(array $log = []): bool
288
290
289
291
protected function formatMessage (string $ format , string $ level , string $ messages , ?array $ context = null ): string
290
292
{
291
- /** @var \DateTimeInterface $date */
293
+ if (!str_contains ($ format , '{ ' )) {
294
+ return "[ {$ level }] {$ messages }" ;
295
+ }
296
+ /** @var \DateTimeInterface|null $date */
292
297
$ date = $ context ["\0_t " ] ?? null ;
298
+ /** @var int|null $index */
299
+ $ index = $ context ["\0_i " ] ?? null ;
293
300
294
301
$ replace = [
295
302
'{date} ' => $ date ? $ date ->format ($ this ->config ['time_format ' ]) : '' ,
296
303
'{level} ' => $ level ,
304
+ '{index} ' => $ index ,
297
305
'{pid} ' => getmypid (),
298
306
'{message} ' => $ messages ,
299
307
];
0 commit comments