Skip to content
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
69 changes: 65 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,94 @@ use TinyBlocks\Logger\Redactions\PhoneRedaction;
PhoneRedaction::from(fields: ['phone', 'mobile', 'whatsapp'], visibleSuffixLength: 4);
```

#### Password redaction

Masks the entire value. No characters are preserved.

```php
use TinyBlocks\Logger\StructuredLogger;
use TinyBlocks\Logger\Redactions\PasswordRedaction;

$logger = StructuredLogger::create()
->withComponent(component: 'auth-service')
->withRedactions(PasswordRedaction::default())
->build();

$logger->info(message: 'login.attempt', context: ['password' => 's3cr3t!']);
# password → "*******"
```

With custom fields:

```php
use TinyBlocks\Logger\Redactions\PasswordRedaction;

PasswordRedaction::from(fields: ['password', 'secret', 'token']);
```

#### Name redaction

Preserves the first N characters (default: 2) and masks the rest.

```php
use TinyBlocks\Logger\StructuredLogger;
use TinyBlocks\Logger\Redactions\NameRedaction;

$logger = StructuredLogger::create()
->withComponent(component: 'user-service')
->withRedactions(NameRedaction::default())
->build();

$logger->info(message: 'user.created', context: ['name' => 'Gustavo']);
# name → "Gu*****"
```

With custom fields and visible length:

```php
use TinyBlocks\Logger\Redactions\NameRedaction;

NameRedaction::from(fields: ['name', 'full_name', 'firstName'], visiblePrefixLength: 3);
# "Gustavo" → "Gus****"
# "Gustavo Freze" → "Gus**********"
# "Maria" → "Mar**"
```

#### Composing multiple redactions

```php
use TinyBlocks\Logger\StructuredLogger;
use TinyBlocks\Logger\Redactions\DocumentRedaction;
use TinyBlocks\Logger\Redactions\EmailRedaction;
use TinyBlocks\Logger\Redactions\NameRedaction;
use TinyBlocks\Logger\Redactions\PasswordRedaction;
use TinyBlocks\Logger\Redactions\PhoneRedaction;

$logger = StructuredLogger::create()
->withComponent(component: 'user-service')
->withRedactions(
DocumentRedaction::default(),
EmailRedaction::default(),
PhoneRedaction::default()
PhoneRedaction::default(),
PasswordRedaction::default(),
NameRedaction::default()
)
->build();

$logger->info(message: 'user.registered', context: [
'document' => '12345678900',
'email' => 'john@example.com',
'phone' => '+5511999887766',
'name' => 'John'
'password' => 's3cr3t!',
'name' => 'John',
'status' => 'active'
]);
# document → "********900"
# email → "jo**@example.com"
# phone → "**********7766"
# name → "John" (unchanged)
# password → "*******"
# name → "Jo**"
# status → "active" (unchanged)
```

#### Custom redaction
Expand Down Expand Up @@ -274,4 +335,4 @@ Logger is licensed under [MIT](LICENSE).
## Contributing

Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to
contribute to the project.
contribute to the project.
41 changes: 41 additions & 0 deletions src/Redactions/NameRedaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Logger\Redactions;

use TinyBlocks\Logger\Internal\Redactor\Redactor;
use TinyBlocks\Logger\Redaction;

final readonly class NameRedaction implements Redaction
{
private const int DEFAULT_VISIBLE_PREFIX_LENGTH = 2;

private Redactor $redactor;

private function __construct(array $fields, int $visiblePrefixLength)
{
$this->redactor = new Redactor(
fields: $fields,
maskingFunction: static function (string $value) use ($visiblePrefixLength): string {
$maskedLength = max(0, strlen($value) - $visiblePrefixLength);
return sprintf('%s%s', substr($value, 0, $visiblePrefixLength), str_repeat('*', $maskedLength));
}
);
}

public static function from(array $fields, int $visiblePrefixLength): NameRedaction
{
return new NameRedaction(fields: $fields, visiblePrefixLength: $visiblePrefixLength);
}

public static function default(): NameRedaction
{
return self::from(fields: ['name'], visiblePrefixLength: self::DEFAULT_VISIBLE_PREFIX_LENGTH);
}

public function redact(array $data): array
{
return $this->redactor->redact(data: $data);
}
}
38 changes: 38 additions & 0 deletions src/Redactions/PasswordRedaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Logger\Redactions;

use TinyBlocks\Logger\Internal\Redactor\Redactor;
use TinyBlocks\Logger\Redaction;

final readonly class PasswordRedaction implements Redaction
{
private Redactor $redactor;

private function __construct(array $fields)
{
$this->redactor = new Redactor(
fields: $fields,
maskingFunction: static function (string $value): string {
return str_repeat('*', strlen($value));
}
);
}

public static function from(array $fields): PasswordRedaction
{
return new PasswordRedaction(fields: $fields);
}

public static function default(): PasswordRedaction
{
return self::from(fields: ['password']);
}

public function redact(array $data): array
{
return $this->redactor->redact(data: $data);
}
}
Loading