11
11
use yii \base \Component ;
12
12
use yii \base \InvalidConfigException ;
13
13
use yii \helpers \ArrayHelper ;
14
+ use yii \helpers \StringHelper ;
14
15
use yii \helpers \VarDumper ;
15
16
use yii \web \Request ;
16
17
@@ -161,6 +162,54 @@ public function collect($messages, $final)
161
162
}
162
163
}
163
164
165
+ /**
166
+ * Flattens a multidimensional array into a one-dimensional array.
167
+ *
168
+ * This method recursively traverses the input array and concatenates the keys
169
+ * to form a new key in the resulting array.
170
+ *
171
+ * Example:
172
+ *
173
+ * ```php
174
+ * $array = [
175
+ * 'A' => [1, 2],
176
+ * 'B' => [
177
+ * 'C' => 1,
178
+ * 'D' => 2,
179
+ * ],
180
+ * 'E' => 1,
181
+ * ];
182
+ * $result = \yii\log\Target::flatten($array);
183
+ * // result will be:
184
+ * // [
185
+ * // 'A.0' => 1
186
+ * // 'A.1' => 2
187
+ * // 'B.C' => 1
188
+ * // 'B.D' => 2
189
+ * // 'E' => 1
190
+ * // ]
191
+ * ```
192
+ *
193
+ * @param array $array the input array to be flattened.
194
+ * @param string $prefix the prefix to be added to each key in the resulting array.
195
+ *
196
+ * @return array the flattened array.
197
+ */
198
+ private static function flatten ($ array , $ prefix = '' ): array
199
+ {
200
+ $ result = [];
201
+
202
+ foreach ($ array as $ key => $ value ) {
203
+ if (is_array ($ value )) {
204
+ $ result = array_merge ($ result , self ::flatten ($ value , $ prefix . $ key . '. ' ));
205
+ } else {
206
+ $ result [$ prefix . $ key ] = $ value ;
207
+ }
208
+ }
209
+
210
+ return $ result ;
211
+ }
212
+
164
213
/**
165
214
* Generates the context information to be logged.
166
215
* The default implementation will dump user information, system variables, etc.
@@ -169,9 +218,12 @@ public function collect($messages, $final)
169
218
protected function getContextMessage ()
170
219
{
171
220
$ context = ArrayHelper::filter ($ GLOBALS , $ this ->logVars );
221
+ $ items = self ::flatten ($ context );
172
222
foreach ($ this ->maskVars as $ var ) {
173
- if (ArrayHelper::getValue ($ context , $ var ) !== null ) {
174
- ArrayHelper::setValue ($ context , $ var , '*** ' );
223
+ foreach ($ items as $ key => $ value ) {
224
+ if (StringHelper::matchWildcard ($ var , $ key , ['caseSensitive ' => false ])) {
225
+ ArrayHelper::setValue ($ context , $ key , '*** ' );
226
+ }
175
227
}
176
228
}
177
229
$ result = [];
@@ -292,7 +344,7 @@ public static function filterMessages($messages, $levels = 0, $categories = [],
292
344
*/
293
345
public function formatMessage ($ message )
294
346
{
295
- list ( $ text , $ level , $ category , $ timestamp) = $ message ;
347
+ [ $ text , $ level , $ category , $ timestamp] = $ message ;
296
348
$ level = Logger::getLevelName ($ level );
297
349
if (!is_string ($ text )) {
298
350
// exceptions may not be serializable if in the call stack somewhere is a Closure
0 commit comments