PHP code example of assimtech / dislog-bundle

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


$request = '<request />';

/** @var \Assimtech\Dislog\ApiCallLoggerInterface $apiCallLogger */
$apiCall = $apiCallLogger->logRequest($request, $endpoint, $appMethod, $reference);

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

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

/**
 * @var \Psr\Http\Message\RequestInterface $request
 * @var \Assimtech\Dislog\LoggingHttpClientInterface $httpClient
 * @var \Psr\Http\Message\ResponseInterface $response
 */
$response = $httpClient->sendRequest(
    $request,
    $appMethod,
    $reference,
    $requestProcessors,
    $responseProcessors,
    $deferredLogging
);

namespace App\Dislog;

use Assimtech\Dislog\Model\ApiCallInterface;
use Assimtech\Dislog\ApiCallLogger;

class ApiLogger extends ApiCallLogger
{
    public function logRequest(
        ?string $request,
        ?string $endpoint,
        ?string $appMethod,
        string $reference = null,
        /* callable[]|callable */ $processors = []
    ): ApiCallInterface {
        $processedRequest = $this->processPayload($processors, $request);

        $apiCall = $this->apiCallFactory->create();
        $apiCall
            ->setRequest($processedRequest)
            ->setEndpoint($endpoint)
            ->setMethod($appMethod)
            ->setReference($reference)
            ->setRequestTime(microtime(true))
        ;

        // Persist $apiCall somewhere

        return $apiCall;
    }

    public function logResponse(
        ApiCallInterface $apiCall,
        string $response = null,
        /* callable[]|callable */ $processors = []
    ): void {
        $duration = microtime(true) - $apiCall->getRequestTime();

        $processedResponse = $this->processPayload($processors, $response);

        $apiCall
            ->setResponse($processedResponse)
            ->setDuration($duration)
        ;

        // Update the persisted $apiCall
    }
}