13
13
use Symfony \Component \VarDumper \Dumper \AbstractDumper ;
14
14
use Symfony \Component \VarDumper \Dumper \CliDumper ;
15
15
use Symfony \Component \VarDumper \Dumper \ContextualizedDumper ;
16
+ use Throwable ;
16
17
use YaPro \MonologExt \Processor \AddStackTraceOfCallPlaceProcessor ;
17
18
use YaPro \MonologExt \VarHelper ;
18
19
use function is_numeric ;
@@ -51,6 +52,7 @@ class JsonToStdErrHandler extends AbstractProcessingHandler
51
52
private int $ maxRecordLength = self ::MAX_RECORD_LENGTH_DEFAULT ;
52
53
53
54
private VarCloner $ varCloner ;
55
+ private CliDumper $ varDumper ;
54
56
55
57
public function __construct (int $ maxRecordLength = 0 ) {
56
58
parent ::__construct ();
@@ -115,6 +117,25 @@ public function handle(array $record): bool
115
117
return false ;
116
118
}
117
119
120
+ public function getDebugInfo (string $ prefix , $ objectOrArray , int $ level = 0 ): string
121
+ {
122
+ // сначала печатаем примитивные типы до уровня 3, а затем все остальные в виде дампов:
123
+ if ($ level === 3 ) {
124
+ return $ prefix .' : ' .$ this ->dump ($ objectOrArray , 1 ) . PHP_EOL ;
125
+ }
126
+ $ result = '' ;
127
+ is_array ($ objectOrArray ) && asort ($ objectOrArray );
128
+ foreach ($ objectOrArray as $ key => $ value ) {
129
+ $ fieldName = $ prefix === '' ? $ key : $ prefix .'. ' . $ key ;
130
+ if (is_scalar ($ value ) || is_null ($ value )) {
131
+ $ result .= trim ($ fieldName .' : ' . $ value ) . PHP_EOL ;
132
+ } else {
133
+ $ result .= $ this ->getDebugInfo ($ fieldName , $ value , $ level + 1 );
134
+ }
135
+ }
136
+ return $ result ;
137
+ }
138
+
118
139
/**
119
140
* @throws JsonException
120
141
*/
@@ -126,24 +147,20 @@ public function write(array $record): void
126
147
127
148
$ result .= $ record ['message ' ] . PHP_EOL ;
128
149
unset($ record ['message ' ]);
129
-
150
+
130
151
$ stackTraceOfCallPlaceProcessor = new AddStackTraceOfCallPlaceProcessor ();
131
152
$ trace = (new Exception ())->getTrace ();
132
153
$ stackTraceBeforeMonolog = $ stackTraceOfCallPlaceProcessor ->getStackTraceBeforeMonolog ($ trace );
133
154
$ callPlace = reset ($ stackTraceBeforeMonolog );
134
155
$ result .= 'The log entry has been wrote by ' . ($ callPlace ['file ' ] ?? '' ) . ': ' . ($ callPlace ['line ' ] ?? '' ) . PHP_EOL ;
135
-
136
- foreach ($ record ['context ' ] as $ key => $ value ) {
137
- $ result .= trim ($ key .' : ' . $ this ->dump ($ value )) . PHP_EOL ;
138
- }
139
- unset($ record ['context ' ]);
140
-
141
- foreach ($ record as $ key => $ value ) {
142
- $ result .= trim ($ key .' : ' . $ this ->dump ($ value )) . PHP_EOL ;
143
- }
156
+
157
+ $ result .= $ this ->getDebugInfo ('' , $ record );
144
158
// $message .= json_encode($this->varHelper->dump($record), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
145
- fwrite ($ this ->stderr , $ result . PHP_EOL );
146
- $ this ->writeToStdErr ($ result );
159
+ if (PHP_SAPI === 'fpm-fcgi ' ) {
160
+ echo '<pre> ' .$ result ;
161
+ } else {
162
+ $ this ->writeToStdErr ($ result );
163
+ }
147
164
exit (123 );
148
165
};
149
166
$ result = $ this ->getMessage ($ record );
@@ -193,7 +210,7 @@ public function dumpRecordDataOnTheLevel(array $record, $maxLevel, $currentLevel
193
210
return $ record ;
194
211
}
195
212
196
- public function reduceRecordDataOnTheLevel (array &$ record , $ maxLevel , $ currentLevel = 1 , & $ changeableRecord )
213
+ public function reduceRecordDataOnTheLevel (array &$ record , $ maxLevel , $ currentLevel = 1 )
197
214
{
198
215
if ($ currentLevel === $ maxLevel ) {
199
216
$ reversed = array_reverse ($ record , true );
@@ -207,7 +224,7 @@ public function reduceRecordDataOnTheLevel(array &$record, $maxLevel, $currentLe
207
224
} else {
208
225
foreach ($ record as $ key => $ value ) {
209
226
if (is_iterable ($ value )) {
210
- $ record [$ key ] = $ this ->reduceRecordDataOnTheLevel ($ value , $ maxLevel , $ currentLevel ++, $ changeableRecord );
227
+ $ record [$ key ] = $ this ->reduceRecordDataOnTheLevel ($ value , $ maxLevel , $ currentLevel ++);
211
228
} else {
212
229
$ record [$ key ] = $ value ;
213
230
}
@@ -235,7 +252,7 @@ public function getMessage(array $record): string
235
252
// попробуем не укорачивать глобально по уровню, а попробуем укоротить уменьшая даннные на уровне $maxDumpLevel+1
236
253
// Для этого сначала задампим данные на уровне $maxDumpLevel+1, а потом будем отбрасывать значения
237
254
$ dumpedRecord = $ this ->dumpRecordDataOnTheLevel ($ record , $ maxDumpLevel +1 );
238
- $ this ->reduceRecordDataOnTheLevel ($ dumpedRecord , $ maxDumpLevel +1 , 1 , $ dumpedRecord );
255
+ $ this ->reduceRecordDataOnTheLevel ($ dumpedRecord , $ maxDumpLevel +1 , 1 );
239
256
240
257
return $ this ->getJson ($ dumpedRecord );
241
258
/*
0 commit comments