PHP code example of assimtech / dislog

1. Go to this page and download the library: Download assimtech/dislog 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/ */

    

assimtech / dislog example snippets


/**
 * @var Psr\Http\Client\ClientInterface $httpClient
 * @var Assimtech\Dislog\ApiCallLoggerInterface $apiCallLogger
 */
$loggingHttpClient = new Assimtech\Dislog\LoggingHttpClient(
    $httpClient,
    $apiCallLogger
);

/**
 * @var Psr\Http\Message\ResponseInterface $response
 */
$response = $loggingHttpClient->sendRequest(
    /* Psr\Http\Message\RequestInterface */ $request,
    /* ?string */ $appMethod = null, // The method in the application that triggered this API call, setting to null will disable API logging
    /* ?string */ $reference = null, // The reference for this specific call (e.g. id or key if available), helps with searching API logs
    /* callable[]|callable|null */ $requestProcessors = null, // Processors to apply to $request, see Processors section below
    /* callable[]|callable|null */ $responseProcessors = null, // Processors to apply to $response, see Processors section below
    /* bool */ $omitPayload = false, // If set to true, request/response will not be logged however metadata will, useful for monitoring without logging full request or response
);

$response = $loggingHttpClient->sendRequest(
    $request,
    $appMethod,
    $reference,
    $requestProcessors,
    $responseProcessors,
    true, // $omitPayload - Do not log raw request / response but still log all other metadata, this allows us to still monitor api calls
);
if (200 !== $response->getStatusCode()) {
    $loggingHttpClient->logLastPayload();
}

/**
 * @var Assimtech\Dislog\ApiCallLoggerInterface $apiCallLogger
 * @var Assimtech\Dislog\Model\ApiCallInterface $apiCall
 */
$apiCall = $apiCallLogger->logRequest(
    /* ?string */ $request,
    /* ?string */ $endpoint,
    /* ?string */ $appMethod,
    /* ?string */ $reference,
    /* callable[]|callable|null */ $processors
);

$response = $api->transmit($request);

$this->apiCallLogger->logResponse($apiCall, $response);

use Assimtech\Dislog;

class Api
{
    protected $apiLogger;

    public function __construct(Dislog\ApiCallLoggerInterface $apiCallLogger)
    {
        $this->apiCallLogger = $apiCallLogger;
    }

    public function transmit($request)
    {
        return '<some response />';
    }

    public function doSomething()
    {
        $request = '<some request />';
        $endpoint = 'http://my.endpoint';
        $reference = time();

        $apiCall = $this->apiCallLogger->logRequest(
            $request,
            $endpoint,
            __METHOD__,
            $reference
        );

        $response = $this->transmit($request);

        $this->apiCallLogger->logResponse(
            $apiCall,
            $response
        );
    }
}

$stream = fopen('/tmp/my.log', 'a');
$uniqueIdentity = new Dislog\Identity\UniqueIdGenerator();
$stringSerializer = new Dislog\Serializer\StringSerializer();
$streamHandler = new Dislog\Handler\Stream($stream, $uniqueIdentity, $stringSerializer);
$apiCallFactory = new Dislog\Factory\ApiCallFactory();
$apiCallLogger = new Dislog\ApiCallLogger($apiCallFactory, $streamHandler);

$api = new Api($apiCallLogger);
$api->doSomething();

$handler->remove(60 * 60 * 24 * 30); // keep 30 days worth of logs

use Assimtech\Dislog;

$stream = fopen('/tmp/my.log', 'a');
$uniqueIdentity = new Dislog\Identity\UniqueIdGenerator();
$stringSerializer = new Dislog\Serializer\StringSerializer();

$streamHandler = new Dislog\Handler\Stream($stream, $uniqueIdentity, $stringSerializer);

$documentHandler = new Dislog\Handler\DoctrineDocumentManager($documentManager);

$entityHandler = new Dislog\Handler\DoctrineEntityManager($entityManager);

function getMaskedCard($card)
{
    $firstSix = substr($card, 0, 6);
    $lastFour = substr($card, -4);
    $middle = str_repeat('*', strlen($card) - 10);
    return $firstSix . $middle . $lastFour;
}

$endpoint = 'https://my.endpoint';
$appMethod = 'processPayment';
$reference = time();
$card = '4444333322221111';
$cvv = '123';
$request = json_encode([
    'amount' => 12.95,
    'card' => $card,
    'expiry' => '2021-04',
    'cvv' => $cvv,
]);

$maskCard = function ($request) use ($card) {
    $maskedCard = getMaskedCard($card);
    return str_replace($card, $maskedCard, $request);
};
$obfuscateCvv = function ($request) use ($cvv) {
    return str_replace($cvv, '***', $request);
};
$apiCallLogger->logRequest(
    $request,
    $endpoint,
    $appMethod,
    $reference,
    [
        $maskCard,
        $obfuscateCvv,
    )
);

$maskedCard = getMaskedCard($card);
$obfuscatedCvv = '***';
$stringReplace = new Assimtech\Dislog\Processor\StringReplace([
    $card,
    $cvv,
], [
    $maskedCard,
    $obfuscatedCvv,
]);
$apiCallLogger->logRequest(
    $request,
    $endpoint,
    $appMethod,
    $reference,
    $stringReplace
);

$response = '{social_security_number: "1234567890"}';
$regexReplace = new Assimtech\Dislog\Processor\RegexReplace(
    '/social_security_number: "(\d\d)\d+(\d\d)"/',
    'social_security_number: "$1***$2"'
);
$apiCallLogger->logResponse(
    $apiCall,
    $response,
    $regexReplace
);