1515use Monolog \Level ;
1616use Monolog \LogRecord ;
1717use Symfony \Component \Console \Formatter \OutputFormatter ;
18+ use Symfony \Component \DependencyInjection \Tests \Compiler \I ;
1819use Symfony \Component \VarDumper \Cloner \Data ;
1920use Symfony \Component \VarDumper \Cloner \Stub ;
2021use Symfony \Component \VarDumper \Cloner \VarCloner ;
@@ -68,6 +69,11 @@ public function __construct(array $options = [])
6869'multiline ' =>false ,
6970'level_name_format ' =>'%-9s ' ,
7071'ignore_empty_context_and_extra ' =>true ,
72+ /**
73+ * @internal option
74+ * @see Symfony\Bridge\Monolog\Command\ServerLogCommand::displayLog
75+ */
76+ 'from_server_log ' =>false ,
7177 ],$ options );
7278
7379if (class_exists (VarCloner::class)) {
@@ -98,30 +104,35 @@ public function formatBatch(array $records): mixed
98104
99105public function format (LogRecord $ record ):mixed
100106 {
101- $ record =$ record ->toArray ();
102107$ record =$ this ->replacePlaceHolder ($ record );
103108
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 );
106115 }else {
107116$ context ='' ;
108117 }
109118
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 );
112125 }else {
113126$ extra ='' ;
114127 }
115128
116129$ 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 ()),
122133'%end_tag% ' =>'</> ' ,
123- '%channel% ' =>$ record[ ' channel ' ] ,
124- '%message% ' =>$ this ->replacePlaceHolder ($ record )[ ' message ' ] ,
134+ '%channel% ' =>$ record-> channel ,
135+ '%message% ' =>$ this ->replacePlaceHolder ($ record )-> message ,
125136'%context% ' =>$ context ,
126137'%extra% ' =>$ extra ,
127138 ]);
@@ -156,15 +167,15 @@ public function castObject(mixed $v, array $a, Stub $s, bool $isNested): array
156167return $ a ;
157168 }
158169
159- private function replacePlaceHolder (array $ record ):array
170+ private function replacePlaceHolder (LogRecord $ record ):LogRecord
160171 {
161- $ message =$ record[ ' message ' ] ;
172+ $ message =$ record-> message ;
162173
163174if (!str_contains ($ message ,'{ ' )) {
164175return $ record ;
165176 }
166177
167- $ context =$ record[ ' context ' ] ;
178+ $ context =$ record-> context ;
168179
169180$ replacements = [];
170181foreach ($ contextas $ k =>$ v ) {
@@ -174,9 +185,7 @@ private function replacePlaceHolder(array $record): array
174185$ replacements ['{ ' .$ k .'} ' ] =sprintf ('<comment>%s</> ' ,$ v );
175186 }
176187
177- $ record ['message ' ] =strtr ($ message ,$ replacements );
178-
179- return $ record ;
188+ return $ record ->with (message:strtr ($ message ,$ replacements ));
180189 }
181190
182191private function dumpData (mixed $ data ,bool $ colors =null ):string