Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mask fields #41

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/Logger/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class Log
*/
protected array $breadcrumbs = [];

/**
* @var array<string> (optional)
*/
protected array $masked = [];

/**
* Log constructor.
*/
Expand Down Expand Up @@ -299,7 +304,7 @@ public function addTag(string $key, string $value): void
*/
public function getTags(): array
{
return $this->tags;
return $this->mask($this->tags);
}

/**
Expand All @@ -321,7 +326,7 @@ public function addExtra(string $key, mixed $value): void
*/
public function getExtra(): array
{
return $this->extra;
return $this->mask($this->extra);
}

/**
Expand Down Expand Up @@ -365,4 +370,39 @@ public function getBreadcrumbs(): array
{
return $this->breadcrumbs;
}

/**
* Set masked fields, which will be replaced by asterisks
*
* @param array<string> $masked
* @return void
*/
public function setMasked(array $masked): void
{
$this->masked = $masked;
}

/**
* @template T
*
* @param array<string, T> $data
* @return array<string, T|string>
*/
private function mask(array $data): array
{
$masked = [];

foreach ($data as $key => $value) {
if (is_string($value) && in_array($key, $this->masked, true)) {
$masked[$key] = str_repeat('*', strlen($value));
} elseif (is_array($value)) {
$maskedValue = $this->mask($value); /** @var T $maskedValue */
$masked[$key] = $maskedValue;
} else {
$masked[$key] = $value;
}
}

return $masked;
}
}
27 changes: 27 additions & 0 deletions tests/unit/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,31 @@ public function testLog(): void
self::assertEquals('DELETE /api/v1/database/abcd1234/efgh5678', $log->getBreadcrumbs()[0]->getMessage());
self::assertEquals($timestamp, $log->getBreadcrumbs()[0]->getTimestamp());
}

public function testLogMasked(): void
{
$log = new Log();

$log->addTag('password', '123456');
$log->addExtra('name', 'John Doe');

self::assertEquals(['password' => '123456'], $log->getTags());
self::assertEquals(['name' => 'John Doe'], $log->getExtra());

$log->setMasked(['password', 'name']);

self::assertEquals(['password' => '******'], $log->getTags());
self::assertEquals(['name' => '********'], $log->getExtra());

// test nested array
$log->addExtra('user', ['password' => 'abc']);

self::assertEquals(['password' => '***'], $log->getExtra()['user']);

// test remove mask
$log->setMasked([]);

self::assertEquals(['password' => '123456'], $log->getTags());
self::assertEquals(['name' => 'John Doe', 'user' => ['password' => 'abc']], $log->getExtra());
christyjacob4 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading