PHP code example of lezhnev74 / psr-logging-masking-middleware

1. Go to this page and download the library: Download lezhnev74/psr-logging-masking-middleware 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/ */

    

lezhnev74 / psr-logging-masking-middleware example snippets


use Lezhnev74\PsrLoggingMaskingMiddleware\MaskingConfig;
use Lezhnev74\PsrLoggingMaskingMiddleware\MessageLogger;
use Lezhnev74\PsrLoggingMaskingMiddleware\MessageMasker;

$logger = new MessageLogger(
    $psr3Logger,                        // inject your app's logger
    new MessageMasker(
        MaskingConfig::create(          // applied to request AND response
            headerNames: ['Authorization', 'Set-Cookie'],
            queryNames: ['api_key'],
            bodyKeys: ['password'],
        ),
    ),
);

use Lezhnev74\PsrLoggingMaskingMiddleware\MaskingConfig;
use Lezhnev74\PsrLoggingMaskingMiddleware\MaskTarget;
use Lezhnev74\PsrLoggingMaskingMiddleware\MessageLoggerBuilder;
use Psr\Log\LogLevel;

$logger = MessageLoggerBuilder::for($psr3Logger)
    ->withMaskingConfig(MaskingConfig::create(
        headerNames: ['Authorization', 'Set-Cookie'],
        queryNames: ['api_key'],
        bodyKeys: ['password', 'card.number'],
    ))
    ->placeholder('[redacted]')          // or ->replaceWith(fn (MaskTarget $t) => '***')
    ->logLevel(LogLevel::INFO)           // defaults to debug
    // ->streamFactory($psr17Factory)    // optional; discovered when omitted
    ->build();

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Lezhnev74\PsrLoggingMaskingMiddleware\HandlerMiddleware;

$stack = HandlerStack::create();
$stack->push(HandlerMiddleware::for($logger));

$client = new Client(['handler' => $stack]);