PHP code example of forci / http-logger-bundle

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

    

forci / http-logger-bundle example snippets




/** @var \Forci\Bundle\HttpLogger\Logger\AbstractLogger $manager */
$manager = $this->container->get('some.logger');
/** @var \Forci\Bundle\HttpLogger\Entity\RequestLog $log */
$log = $manager->log('This is some message with any information that would eventually help you once you need to debug something');

try {
    $client = new \GuzzleHttp\Client();
    $request = new \GuzzleHttp\Psr7\Request('GET', 'http://some-website.com/');

    $manager->logRequest($log, $request, \Forci\Bundle\HttpLogger\Entity\RequestLogMessageType::ID_TEXT_PLAIN);

    $response = $client->send($request);

    $manager->logResponse($log, $response, \Forci\Bundle\HttpLogger\Entity\RequestLogMessageType::ID_HTML);

    $ex = new \Exception('First Exception');

    throw new \Exception('Second Exception', 0, $ex);
} catch (\Throwable $e) {
    $manager->logException($log, $e);
}



$log = $this->log('SomeClass::someMethod()');

try {
    $request = new \GuzzleHttp\Psr7\Request('POST', 'https://someUri.com/API', [
        \GuzzleHttp\RequestOptions::BODY => 'SomeBody'
    ]);

    $this->logRequest($log, $request, \Forci\Bundle\HttpLogger\Entity\RequestLogMessageType::ID_XML);

    $this->pool->sendAsync($request, function (\Psr\Http\Message\ResponseInterface $response) use ($log) {
        try {
            $rawResponse = $response->getBody()->getContents();

            $this->logResponse($log, $response, \Forci\Bundle\HttpLogger\Entity\RequestLogMessageType::ID_XML);

            /** @var \Symfony\Component\DomCrawler\Crawler $crawler */
            $crawler = new \Symfony\Component\DomCrawler\Crawler($rawResponse);

            try {
                $crawler->filterXPath('//some/path');
                // do some Crawler work
            } catch (\InvalidArgumentException $ex) {
                $this->exception($log, $ex, $crawler->html());
            }
        } catch (\Throwable $ex) {
            $this->exception($log, $ex);
        }
    }, function (\GuzzleHttp\Exception\RequestException $ex) use ($log) {
        $this->requestException($log, $ex, \Forci\Bundle\HttpLogger\Entity\RequestLogMessageType::ID_XML);
    });
} catch (\Throwable $ex) {
    $this->exception($log, $ex);
}



// Add this to your AppKernel.php
$bundles = [
    // ...
    new \Forci\Bundle\HttpLogger\ForciHttpLoggerBundle(),
    // ...
];



namespace Some\Name\Space;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="SomeRepositoryClass")
 */
class YourRequestLog extends \Forci\Bundle\HttpLogger\Entity\RequestLog {

    /**
     * @ORM\ManyToOne(targetEntity="Some\Name\Space\SomeOtherEntity", inversedBy="inverseSideField")
     * @ORM\JoinColumn(name="relation_id", referencedColumnName="id", nullable=alse)
     */
    protected $someOtherEntity;
    
}    



namespace App\Logger;

// any other use, dopped for brevity
use Forci\Bundle\HttpLogger\Logger\AbstractLogger;

class YourRequestLogLogger extends AbstractLogger {

    /**
     * @return YourRequestLog
     */
    public function createLog() {
        return new YourRequestLog();
    }

    /**
     * @return RequestLogMessage
     */
    public function createLogMessage() {
        return new RequestLogMessage();
    }

    /**
     * @return RequestLogException
     */
    public function createLogException() {
        return new RequestLogException();
    }

    /**
     * @return RequestLogMessageType
     */
    public function createLogMessageType() {
        return new RequestLogMessageType();
    }

    /**
     * @return YourRequestLog
     */
    public function log(string $msg, SomeOtherEntity $entity) {
        /** @var RequestLog $log */
        $log = parent::_log($msg);
        
        $log->setSomeOtherEntity($entity);

        $this->save($log);

        return $log;
    }

}