15
15
use Monolog \Level ;
16
16
use Monolog \LogRecord ;
17
17
use Symfony \Component \Console \Formatter \OutputFormatter ;
18
+ use Symfony \Component \DependencyInjection \Tests \Compiler \I ;
18
19
use Symfony \Component \VarDumper \Cloner \Data ;
19
20
use Symfony \Component \VarDumper \Cloner \Stub ;
20
21
use Symfony \Component \VarDumper \Cloner \VarCloner ;
@@ -68,6 +69,11 @@ public function __construct(array $options = [])
68
69
'multiline ' => false ,
69
70
'level_name_format ' => '%-9s ' ,
70
71
'ignore_empty_context_and_extra ' => true ,
72
+ /**
73
+ * @internal option
74
+ * @see Symfony\Bridge\Monolog\Command\ServerLogCommand::displayLog
75
+ */
76
+ 'from_server_log ' => false ,
71
77
], $ options );
72
78
73
79
if (class_exists (VarCloner::class)) {
@@ -98,30 +104,35 @@ public function formatBatch(array $records): mixed
98
104
99
105
public function format (LogRecord $ record ): mixed
100
106
{
101
- $ record = $ record ->toArray ();
102
107
$ record = $ this ->replacePlaceHolder ($ record );
103
108
104
- if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ['context ' ])) {
105
- $ context = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ record ['context ' ]);
109
+ if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ->context )) {
110
+ $ context = $ record ->context ;
111
+ if ($ this ->options ['from_server_log ' ]) {
112
+ $ context = $ context [0 ];
113
+ }
114
+ $ context = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ context );
106
115
} else {
107
116
$ context = '' ;
108
117
}
109
118
110
- if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ['extra ' ])) {
111
- $ extra = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ record ['extra ' ]);
119
+ if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ->extra )) {
120
+ $ extra = $ record ->extra ;
121
+ if ($ this ->options ['from_server_log ' ]) {
122
+ $ extra = $ extra [0 ];
123
+ }
124
+ $ extra = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ extra );
112
125
} else {
113
126
$ extra = '' ;
114
127
}
115
128
116
129
$ formatted = strtr ($ this ->options ['format ' ], [
117
- '%datetime% ' => $ record ['datetime ' ] instanceof \DateTimeInterface
118
- ? $ record ['datetime ' ]->format ($ this ->options ['date_format ' ])
119
- : $ record ['datetime ' ],
120
- '%start_tag% ' => sprintf ('<%s> ' , self ::LEVEL_COLOR_MAP [$ record ['level ' ]]),
121
- '%level_name% ' => sprintf ($ this ->options ['level_name_format ' ], $ record ['level_name ' ]),
130
+ '%datetime% ' => $ record ->datetime ->format ($ this ->options ['date_format ' ]),
131
+ '%start_tag% ' => sprintf ('<%s> ' , self ::LEVEL_COLOR_MAP [$ record ->level ->value ]),
132
+ '%level_name% ' => sprintf ($ this ->options ['level_name_format ' ], $ record ->level ->getName ()),
122
133
'%end_tag% ' => '</> ' ,
123
- '%channel% ' => $ record[ ' channel ' ] ,
124
- '%message% ' => $ this ->replacePlaceHolder ($ record )[ ' message ' ] ,
134
+ '%channel% ' => $ record-> channel ,
135
+ '%message% ' => $ this ->replacePlaceHolder ($ record )-> message ,
125
136
'%context% ' => $ context ,
126
137
'%extra% ' => $ extra ,
127
138
]);
@@ -156,15 +167,15 @@ public function castObject(mixed $v, array $a, Stub $s, bool $isNested): array
156
167
return $ a ;
157
168
}
158
169
159
- private function replacePlaceHolder (array $ record ): array
170
+ private function replacePlaceHolder (LogRecord $ record ): LogRecord
160
171
{
161
- $ message = $ record[ ' message ' ] ;
172
+ $ message = $ record-> message ;
162
173
163
174
if (!str_contains ($ message , '{ ' )) {
164
175
return $ record ;
165
176
}
166
177
167
- $ context = $ record[ ' context ' ] ;
178
+ $ context = $ record-> context ;
168
179
169
180
$ replacements = [];
170
181
foreach ($ context as $ k => $ v ) {
@@ -174,9 +185,7 @@ private function replacePlaceHolder(array $record): array
174
185
$ replacements ['{ ' .$ k .'} ' ] = sprintf ('<comment>%s</> ' , $ v );
175
186
}
176
187
177
- $ record ['message ' ] = strtr ($ message , $ replacements );
178
-
179
- return $ record ;
188
+ return $ record ->with (message: strtr ($ message , $ replacements ));
180
189
}
181
190
182
191
private function dumpData (mixed $ data , bool $ colors = null ): string
0 commit comments