2017 © Pedro Peláez
 

symfony-bundle http-logger-bundle

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

image

wucdbm/http-logger-bundle

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  • Friday, December 15, 2017
  • by wucdbm
  • Repository
  • 1 Watchers
  • 0 Stars
  • 894 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

Purpose

The purpose of this Bundle is to log HTTP Request/Responses in logs. In addition to that, you can host exceptions (\Throwable). This is especially useful when working with terribly written APIs you have no control of, that tend to easily break libxml (and thus Symfony's Crawler)., (*1)

Presentation

At this point, the bundle has no presentation of the data it collects. You should implement that on your own., (*2)

Basic Usage

$manager = $this->container->get('some.manager');
$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 Request('GET', 'http://some-website.com/');

    $manager->logRequest($log, $request, RequestLogMessageType::ID_TEXT_PLAIN);

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

    $manager->logResponse($log, $response, RequestLogMessageType::ID_HTML);

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

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

Advanced Usage

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

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

    $this->logRequest($log, $request, RequestLogMessageType::ID_XML);

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

            $this->logResponse($log, $response, RequestLogMessageType::ID_XML);

            $crawler = new Crawler($rawResponse);

            try {
                // do some Crawler work
            } catch (\InvalidArgumentException $ex) {
                $this->exception($log, $ex, $crawler->html());
            }
        } catch (\Throwable $ex) {
            $this->exception($log, $ex);
        }
    }, function (RequestException $ex) use ($log) {
        $this->requestException($log, $ex, RequestLogMessageType::ID_XML);
    });
} catch (\Throwable $ex) {
    $this->exception($log, $ex);
}

There is also a method called "logGuzzleException". It is a shorthand for logging the response, if any, upon HTTP 500 and such. Keep in mind that this is a very basic example. The real power of this bundle comes when you have to execute tons of requests asynchronously, without human overview, via curl, and where it is painfully hard to find which one exactly went broke, without proper logging., (*3)

Installation & Setup

config.yml

wucdbm_http_logger:
    configs:
        bookings:
            table_prefix: some_logs__
            log_class: Some\Name\Space\RequestLog
            log_message_class: Some\Name\Space\RequestLogMessage
            log_message_type_class: Some\Name\Space\RequestLogMessageType
            log_exception_class: Some\Name\Space\RequestLogException

AppKernel

new \Wucdbm\Bundle\WucdbmHttpLoggerBundle\WucdbmHttpLoggerBundle(),

You need to extend each of the entities and create your own. You can freely add any additional fields and map them via your preferred method. The base mapping is done via a Subscriber in the bundle., (*4)

<?php

namespace Some\Name\Space;

use Doctrine\ORM\Mapping as ORM;

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

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

}    

Finally, before you can use the logger, you must create a Logger that extends \Wucdbm\Bundle\WucdbmHttpLoggerBundle\Logger\AbstractLogger You must implement the factory methods for creating each of your entities. This may be automated in future versions, so I would advise against creating constructors on these, unless I get enough time and get a proper implementation using an interface and a base factory that just works out of the box., (*5)

<?php

namespace App\Logger;

// any other use, dopped for brevity
use Wucdbm\Bundle\WucdbmHttpLoggerBundle\Logger\AbstractLogger;

class YourRequestLogLogger extends AbstractLogger {

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

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

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

    /**
     * @return RequestLogMessageType
     */
    protected 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;
    }

}

The Versions

15/12 2017

dev-master

9999999-dev

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

20/02 2017

3.0.x-dev

3.0.9999999.9999999-dev

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

20/02 2017

v3.0.14

3.0.14.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

12/12 2016

v3.0.13

3.0.13.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

29/11 2016

v3.0.12

3.0.12.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

29/11 2016

3.0.11

3.0.11.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

29/11 2016

3.0.10

3.0.10.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

29/11 2016

3.0.9

3.0.9.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

08/11 2016

3.0.8

3.0.8.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

08/11 2016

3.0.7

3.0.7.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

08/11 2016

v3.0.6

3.0.6.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

01/11 2016

v3.0.5

3.0.5.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

01/11 2016

v3.0.4

3.0.4.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

01/11 2016

v3.0.3

3.0.3.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

01/11 2016

v3.0.2

3.0.2.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

26/10 2016

3.0.1

3.0.1.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log

26/10 2016

3.0.0

3.0.0.0

A Symfony 3.0 Bundle for hosting HTTP Request/Responses, as well as exceptions that occur during the handling of those

  Sources   Download

GPL-3.0

The Requires

 

by Martin Kirilov

http wucdbm http logger http logger bundle http request response log http log bundle symfony http log