GithubHelp home page GithubHelp logo

guzzle-logger's Introduction

codecov.io Build Status

GuzzleHttpLogger Middleware

This is a middleware for the guzzle that automatically logs every request and response using a PSR-3 logger.

Install

Via Composer

$ composer require gmponos/guzzle-http-logger

Usage

Simple usage

use Gmponos\GuzzleLogger\Middleware\LoggerMiddleware;
use GuzzleHttp\HandlerStack;

$logger = new Logger();  //A new PSR-3 Logger like Monolog
$stack = new HandlerStack();
$stack->push(new LoggerMiddleware($logger));
$client = new GuzzleHttp\Client([
    'handler' => $stack,
]);

From now on each request you make and response that you receive using the $client will be logged. The default levels that the middleware uses for logging are the following.

  • Requests are logged with level debug.
  • Request statistics are logged with level debug.
  • Responses with http status code < 400 with level debug.
  • Responses with http status code < 500 with level error.
  • Responses with http status code >= 500 with level critical.

Advanced initialization

The signature of the LoggerMiddleware class is the following:

LoggerMiddleware(LoggerInterface $logger, $logRequests = true, $logStatistics = false, array $thresholds = [])

  • logger - The PSR-3 logger to use for logging.
  • logRequests - By default the middleware is set to log every request and response. If you wish that to log only the requests and responses that you retrieve a status code above 4xx set this as false.
  • logRequests - If you set logRequests as true and this as true then guzzle will also log statistics about the requests.
  • thresholds - An array that you may use to change the thresholds of logging the responses.

Using options on each request

You can set on each request options about your log.

$client->get("/", [
    'log' => [
        'requests' => true,
        'statistics' => true,
        'error_threshold' => null,
        'warning_threshold' => null,
        'levels' => [
            400 => 'info'
            401 => 'warning'
            ...
        ]
    ]
]);
  • requests Do not log anything unless if the request is above the threshold or inside the levels.
  • statistics if the requests variable is true and this is also true the logger will log statistics about the request
  • levels set custom log levels for each response status code

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Todo

  • Decouple the format of the record from the middleware.
  • More tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.