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
@@ -91,6 +92,11 @@ abstract class Target extends Component
91
92
* - `var` - `var` will be logged as `***`
92
93
* - `var.key` - only `var[key]` will be logged as `***`
93
94
*
95
+ * In addition, this property accepts (case-insensitive) patterns. For example:
96
+ * - `_SERVER.*_SECRET` matches all ending with `_SECRET`, such as `$_SERVER['TOKEN_SECRET']` etc.
97
+ * - `_SERVER.SECRET_*` matches all starting with `SECRET_`, such as `$_SERVER['SECRET_TOKEN']` etc.
98
+ * - `_SERVER.*SECRET*` matches all containing `SECRET` i.e. both of the above.
99
+ *
94
100
* @since 2.0.16
95
101
*/
96
102
public $ maskVars = [
@@ -161,6 +167,54 @@ public function collect($messages, $final)
161
167
}
162
168
}
163
169
170
+ /**
171
+ * Flattens a multidimensional array into a one-dimensional array.
172
+ *
173
+ * This method recursively traverses the input array and concatenates the keys
174
+ * to form a new key in the resulting array.
175
+ *
176
+ * Example:
177
+ *
178
+ * ```php
179
+ * $array = [
180
+ * 'A' => [1, 2],
181
+ * 'B' => [
182
+ * 'C' => 1,
183
+ * 'D' => 2,
184
+ * ],
185
+ * 'E' => 1,
186
+ * ];
187
+ * $result = \yii\log\Target::flatten($array);
188
+ * // result will be:
189
+ * // [
190
+ * // 'A.0' => 1
191
+ * // 'A.1' => 2
192
+ * // 'B.C' => 1
193
+ * // 'B.D' => 2
194
+ * // 'E' => 1
195
+ * // ]
196
+ * ```
197
+ *
198
+ * @param array $array the input array to be flattened.
199
+ * @param string $prefix the prefix to be added to each key in the resulting array.
200
+ *
201
+ * @return array the flattened array.
202
+ */
203
+ private static function flatten ($ array , $ prefix = '' ): array
204
+ {
205
+ $ result = [];
206
+
207
+ foreach ($ array as $ key => $ value ) {
208
+ if (is_array ($ value )) {
209
+ $ result = array_merge ($ result , self ::flatten ($ value , $ prefix . $ key . '. ' ));
210
+ } else {
211
+ $ result [$ prefix . $ key ] = $ value ;
212
+ }
213
+ }
214
+
215
+ return $ result ;
216
+ }
217
+
164
218
/**
165
219
* Generates the context information to be logged.
166
220
* The default implementation will dump user information, system variables, etc.
@@ -169,9 +223,12 @@ public function collect($messages, $final)
169
223
protected function getContextMessage ()
170
224
{
171
225
$ context = ArrayHelper::filter ($ GLOBALS , $ this ->logVars );
226
+ $ items = self ::flatten ($ context );
172
227
foreach ($ this ->maskVars as $ var ) {
173
- if (ArrayHelper::getValue ($ context , $ var ) !== null ) {
174
- ArrayHelper::setValue ($ context , $ var , '*** ' );
228
+ foreach ($ items as $ key => $ value ) {
229
+ if (StringHelper::matchWildcard ($ var , $ key , ['caseSensitive ' => false ])) {
230
+ ArrayHelper::setValue ($ context , $ key , '*** ' );
231
+ }
175
232
}
176
233
}
177
234
$ result = [];
@@ -292,7 +349,7 @@ public static function filterMessages($messages, $levels = 0, $categories = [],
292
349
*/
293
350
public function formatMessage ($ message )
294
351
{
295
- list ( $ text , $ level , $ category , $ timestamp) = $ message ;
352
+ [ $ text , $ level , $ category , $ timestamp] = $ message ;
296
353
$ level = Logger::getLevelName ($ level );
297
354
if (!is_string ($ text )) {
298
355
// exceptions may not be serializable if in the call stack somewhere is a Closure
0 commit comments