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