PHP code example of phphleb / maskolog

1. Go to this page and download the library: Download phphleb/maskolog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phphleb / maskolog example snippets



$factory = new \Maskolog\ExampleManagedLoggerFactory();
$logger = new \Maskolog\Logger($factory);


$logger = $logger->withMaskingProcessors([PasswordMaskingProcessor::class => 'password']);

$logger->info('Text using password: {password}', ['password' => 'secret_password']);
    
// 'Text using password: *REDACTED.PASSWORD*' if masking is enabled, otherwise 'Text using password: secret_password'.

$logger->withMaskingProcessors([
           StringMaskingProcessor::class => 'token',
           UrlMaskingProcessor::class => 'url',
     ])
    ->info('Text using sensitive data: {token}, {password}, {url}', ['password' => 'secret_password', 'token' => 'secret_token', 'url' => 'domain.ru?hash=secret_hash']);

// 'Text using sensitive data: sec*REDACTED*en, *REDACTED.PASSWORD*, domain.ru?hash=REDACTED' if masking is enabled.


public function withPasswordMasking(array $value = ['password', 'pass']): Logger
{
    return $this->withMaskingProcessors([PasswordMaskingProcessor::class => $value]);
}

$logger->withPasswordMasking() 
       ->info('Text using password: {password}', ['password' => 'secret_password']);

$configValues = array_values(['cdn_url' => 'cdn.site.domain', 'cdn_token' => 'secret_token']);

$this->pushMaskingProcessor(function($record) use ($configValues) {
    return (new ConfigMaskingProcessor($configValues))($record);
});

$logger->withMaskingProcessors(
    [StringMaskingProcessor::class => ['internal' => ['token']]]
    )->info('List of tokens', [
      'internal' => ['token' => 'secret_token'], // The token will be masked
      'external' => ['token' => 'public_token']  // The token will not be masked
    ]);

class ExampleObject {
    public string $examplePassword = 'secret_password';
}

  $logger->withMaskingProcessors(
            [PasswordMaskingProcessor::class => 'examplePassword']
        )->info('Masked object', [new ExampleObject()]);

  $logger->withMaskingProcessors(
            [PasswordMaskingProcessor::class => [
                [ExampleObject::class => ['examplePassword']],
            ]]
        )->info('Masked object', [new ExampleObject()]);

use Maskolog\Attributes\Mask;
class ExampleDto {      
    public function __construct(
         #[\SensitiveParameter]
         #[Mask]         
         readonly public string $secret,
         #[\SensitiveParameter]
         #[Mask(PasswordMaskingProcessor::class)]         
         readonly public string $password;
    ) {}
}

public function index(ExampleLogger $exampleLogger, LoggerInterface $psrLogger): void
{
    $psrLogger->info('Global masks applied only');

    $exampleLogger->withPasswordMasking()
        ->info('Local password masking on top of global masks: {password}', ['password' => 'secret_password']);
}