12
12
namespace Symfony \Bridge \Monolog \Formatter ;
13
13
14
14
use Monolog \Formatter \FormatterInterface ;
15
- use Monolog \Logger ;
15
+ use Monolog \Level ;
16
16
use Monolog \LogRecord ;
17
17
use Symfony \Component \Console \Formatter \OutputFormatter ;
18
18
use Symfony \Component \VarDumper \Cloner \Data ;
28
28
*/
29
29
final class ConsoleFormatter implements FormatterInterface
30
30
{
31
- use CompatibilityFormatter;
32
-
33
31
public const SIMPLE_FORMAT = "%datetime% %start_tag%%level_name%%end_tag% <comment>[%channel%]</> %message%%context%%extra% \n" ;
34
32
public const SIMPLE_DATE = 'H:i:s ' ;
35
33
36
34
private const LEVEL_COLOR_MAP = [
37
- Logger:: DEBUG => 'fg=white ' ,
38
- Logger:: INFO => 'fg=green ' ,
39
- Logger:: NOTICE => 'fg=blue ' ,
40
- Logger:: WARNING => 'fg=cyan ' ,
41
- Logger:: ERROR => 'fg=yellow ' ,
42
- Logger:: CRITICAL => 'fg=red ' ,
43
- Logger:: ALERT => 'fg=red ' ,
44
- Logger:: EMERGENCY => 'fg=white;bg=red ' ,
35
+ Level::Debug-> value => 'fg=white ' ,
36
+ Level::Info-> value => 'fg=green ' ,
37
+ Level::Notice-> value => 'fg=blue ' ,
38
+ Level::Warning-> value => 'fg=cyan ' ,
39
+ Level::Error-> value => 'fg=yellow ' ,
40
+ Level::Critical-> value => 'fg=red ' ,
41
+ Level::Alert-> value => 'fg=red ' ,
42
+ Level::Emergency-> value => 'fg=white;bg=red ' ,
45
43
];
46
44
47
45
private array $ options ;
@@ -98,34 +96,31 @@ public function formatBatch(array $records): mixed
98
96
return $ records ;
99
97
}
100
98
101
- private function doFormat ( array | LogRecord $ record ): mixed
99
+ public function format ( LogRecord $ record ): mixed
102
100
{
103
- if ($ record instanceof LogRecord) {
104
- $ record = $ record ->toArray ();
105
- }
106
101
$ record = $ this ->replacePlaceHolder ($ record );
107
102
108
- if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ['context ' ])) {
109
- $ context = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ record ['context ' ]);
103
+ if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ->context )) {
104
+ $ context = $ record ->context ;
105
+ $ context = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ context );
110
106
} else {
111
107
$ context = '' ;
112
108
}
113
109
114
- if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ['extra ' ])) {
115
- $ extra = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ record ['extra ' ]);
110
+ if (!$ this ->options ['ignore_empty_context_and_extra ' ] || !empty ($ record ->extra )) {
111
+ $ extra = $ record ->extra ;
112
+ $ extra = ($ this ->options ['multiline ' ] ? "\n" : ' ' ).$ this ->dumpData ($ extra );
116
113
} else {
117
114
$ extra = '' ;
118
115
}
119
116
120
117
$ formatted = strtr ($ this ->options ['format ' ], [
121
- '%datetime% ' => $ record ['datetime ' ] instanceof \DateTimeInterface
122
- ? $ record ['datetime ' ]->format ($ this ->options ['date_format ' ])
123
- : $ record ['datetime ' ],
124
- '%start_tag% ' => sprintf ('<%s> ' , self ::LEVEL_COLOR_MAP [$ record ['level ' ]]),
125
- '%level_name% ' => sprintf ($ this ->options ['level_name_format ' ], $ record ['level_name ' ]),
118
+ '%datetime% ' => $ record ->datetime ->format ($ this ->options ['date_format ' ]),
119
+ '%start_tag% ' => sprintf ('<%s> ' , self ::LEVEL_COLOR_MAP [$ record ->level ->value ]),
120
+ '%level_name% ' => sprintf ($ this ->options ['level_name_format ' ], $ record ->level ->getName ()),
126
121
'%end_tag% ' => '</> ' ,
127
- '%channel% ' => $ record[ ' channel ' ] ,
128
- '%message% ' => $ this ->replacePlaceHolder ($ record )[ ' message ' ] ,
122
+ '%channel% ' => $ record-> channel ,
123
+ '%message% ' => $ this ->replacePlaceHolder ($ record )-> message ,
129
124
'%context% ' => $ context ,
130
125
'%extra% ' => $ extra ,
131
126
]);
@@ -160,15 +155,15 @@ public function castObject(mixed $v, array $a, Stub $s, bool $isNested): array
160
155
return $ a ;
161
156
}
162
157
163
- private function replacePlaceHolder (array $ record ): array
158
+ private function replacePlaceHolder (LogRecord $ record ): LogRecord
164
159
{
165
- $ message = $ record[ ' message ' ] ;
160
+ $ message = $ record-> message ;
166
161
167
162
if (!str_contains ($ message , '{ ' )) {
168
163
return $ record ;
169
164
}
170
165
171
- $ context = $ record[ ' context ' ] ;
166
+ $ context = $ record-> context ;
172
167
173
168
$ replacements = [];
174
169
foreach ($ context as $ k => $ v ) {
@@ -178,9 +173,7 @@ private function replacePlaceHolder(array $record): array
178
173
$ replacements ['{ ' .$ k .'} ' ] = sprintf ('<comment>%s</> ' , $ v );
179
174
}
180
175
181
- $ record ['message ' ] = strtr ($ message , $ replacements );
182
-
183
- return $ record ;
176
+ return $ record ->with (message: strtr ($ message , $ replacements ));
184
177
}
185
178
186
179
private function dumpData (mixed $ data , bool $ colors = null ): string
@@ -195,7 +188,9 @@ private function dumpData(mixed $data, bool $colors = null): string
195
188
$ this ->dumper ->setColors ($ colors );
196
189
}
197
190
198
- if (!$ data instanceof Data) {
191
+ if (($ data ['data ' ] ?? null ) instanceof Data) {
192
+ $ data = $ data ['data ' ];
193
+ } elseif (!$ data instanceof Data) {
199
194
$ data = $ this ->cloner ->cloneVar ($ data );
200
195
}
201
196
$ data = $ data ->withRefHandles (false );
0 commit comments