9
9
use Psr \Log \LogLevel ;
10
10
use think \App ;
11
11
use think \contract \LogHandlerInterface ;
12
+ use think \event \LogRecord ;
12
13
use Zxin \SocketLog \SocketClient ;
13
14
14
15
class SocketV2 implements LogHandlerInterface
@@ -123,27 +124,30 @@ public function __construct(App $app, array $config = [])
123
124
}
124
125
}
125
126
126
- protected function logReader (array $ log , bool $ group ): \Generator
127
+ protected function logReader (array $ logs , bool $ group ): \Generator
127
128
{
128
129
// 是否启用兼容模式的备用判断
129
- $ newImplement = $ this ->newImplement ?? array_is_list ($ log );
130
+ $ newImplement = $ this ->newImplement ?? array_is_list ($ logs );
130
131
131
132
if ($ newImplement ) {
132
133
if ($ group ) {
133
134
$ 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 ;
136
138
}
137
139
yield from $ group ;
138
140
} else {
139
- yield from $ log ;
141
+ yield from $ logs ;
140
142
}
141
143
} else {
142
144
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
+ }
144
148
} else {
145
- foreach ($ log as $ type => $ msg ) {
146
- yield [ $ type , $ msg] ;
149
+ foreach ($ logs as $ type => $ msg ) {
150
+ yield new LogRecord ( $ type , $ msg) ;
147
151
}
148
152
}
149
153
}
@@ -196,9 +200,9 @@ public function save(array $log = []): bool
196
200
'msg ' => '[ ' . $ type . ' ] ' ,
197
201
'css ' => $ this ->css [$ type ] ?? '' ,
198
202
];
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 ) }" ;
202
206
$ trace [] = [
203
207
'type ' => 'log ' ,
204
208
'msg ' => $ msg ,
@@ -218,14 +222,14 @@ public function save(array $log = []): bool
218
222
'css ' => '' ,
219
223
];
220
224
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 ;
224
227
$ css = $ this ->css2 [$ type ] ?? '' ;
225
228
if (in_array ($ css , self ::LogLevelSet, true )) {
226
229
$ css = $ this ->css2 [$ css ] ?? '' ;
227
230
}
228
- $ msg = $ format ? $ this ->formatMessage ($ format , $ type , $ messages , $ ctx ) : "[ {$ type }] {$ messages }" ;
231
+
232
+ $ msg = $ format ? $ this ->formatMessage ($ format , $ item ) : "[ {$ type }] {$ this ->normalizeMessage ($ item ->message )}" ;
229
233
$ trace [] = [
230
234
'type ' => 'log ' ,
231
235
'msg ' => $ msg ,
@@ -301,20 +305,20 @@ protected function normalizeMessage($message): string
301
305
return $ message ;
302
306
}
303
307
304
- protected function formatMessage (string $ format , string $ level , string $ messages , ? array $ context = null ): string
308
+ protected function formatMessage (string $ format , LogRecord $ logRecord ): string
305
309
{
310
+ $ level = $ logRecord ->type ;
311
+ $ messages = $ this ->normalizeMessage ($ logRecord ->message );
312
+
306
313
if (!str_contains ($ format , '{ ' )) {
307
314
return "[ {$ level }] {$ messages }" ;
308
315
}
309
316
/** @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 ;
313
318
314
319
$ replace = [
315
320
'{date} ' => $ date ? $ date ->format ($ this ->config ['time_format ' ]) : '' ,
316
321
'{level} ' => $ level ,
317
- '{index} ' => $ index ,
318
322
'{pid} ' => getmypid (),
319
323
'{message} ' => $ messages ,
320
324
];
0 commit comments