@@ -82,6 +82,11 @@ class Log
8282 */
8383 protected array $ breadcrumbs = [];
8484
85+ /**
86+ * @var array<string> (optional)
87+ */
88+ protected array $ masked = [];
89+
8590 /**
8691 * Log constructor.
8792 */
@@ -299,7 +304,7 @@ public function addTag(string $key, string $value): void
299304 */
300305 public function getTags (): array
301306 {
302- return $ this ->tags ;
307+ return $ this ->mask ( $ this -> tags ) ;
303308 }
304309
305310 /**
@@ -321,7 +326,7 @@ public function addExtra(string $key, mixed $value): void
321326 */
322327 public function getExtra (): array
323328 {
324- return $ this ->extra ;
329+ return $ this ->mask ( $ this -> extra ) ;
325330 }
326331
327332 /**
@@ -365,4 +370,39 @@ public function getBreadcrumbs(): array
365370 {
366371 return $ this ->breadcrumbs ;
367372 }
373+
374+ /**
375+ * Set masked fields, which will be replaced by asterisks
376+ *
377+ * @param array<string> $masked
378+ * @return void
379+ */
380+ public function setMasked (array $ masked ): void
381+ {
382+ $ this ->masked = $ masked ;
383+ }
384+
385+ /**
386+ * @template T
387+ *
388+ * @param array<string, T> $data
389+ * @return array<string, T|string>
390+ */
391+ private function mask (array $ data ): array
392+ {
393+ $ masked = [];
394+
395+ foreach ($ data as $ key => $ value ) {
396+ if (is_string ($ value ) && in_array ($ key , $ this ->masked , true )) {
397+ $ masked [$ key ] = str_repeat ('* ' , strlen ($ value ));
398+ } elseif (is_array ($ value )) {
399+ $ maskedValue = $ this ->mask ($ value ); /** @var T $maskedValue */
400+ $ masked [$ key ] = $ maskedValue ;
401+ } else {
402+ $ masked [$ key ] = $ value ;
403+ }
404+ }
405+
406+ return $ masked ;
407+ }
368408}
0 commit comments