php-http / httplug

HTTPlug, the HTTP client abstraction for PHP
http://httplug.io
MIT License
2.57k stars 39 forks source link

Implement message logging #113

Closed jakzal closed 8 years ago

jakzal commented 8 years ago

I have implemented a similar library some time ago ((un)fortunately not open source), but since httplug is now stable I'd like to switch. What I'm missing is logging of requests and responses. I have implemented it as a decorator (see code below). I'll be happy to send a PR if you'd like to include this feature in httplug.

final class LoggingHttpClient implements HttpClient
{
    /**
     * @var HttpClient
     */
    private $httpClient;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param HttpClient      $httpClient
     * @param LoggerInterface $logger
     */
    public function __construct(HttpClient $httpClient, LoggerInterface $logger)
    {
        $this->httpClient = $httpClient;
        $this->logger = $logger;
    }

    public function sendRequest(Request $request)
    {
        try {
            $response = $this->httpClient->sendRequest($request);

            $this->logRequestAndResponse($request, $response);

            return $response;
        } catch (\Exception $e) {
            $this->logRequest($request);

            throw $e;
        }

     // ...
}
xabbuh commented 8 years ago

Is the Logger Plugin what you are looking for?

jakzal commented 8 years ago

Indeed. No idea how I could miss it. Cheers!