Coder Social home page Coder Social logo

loguzz's Introduction

Loguzz codecov Total Downloads Latest Stable Version

Loguzz is a middleware for Guzzle which logs requests and responses.

Installation

You'll need composer to install the package. composer require anik/loguzz

Documentation V1

Find the thorough documentation here.

Documentation V3/V2

To log a request, you'll need to push Loguzz\Middleware\LogMiddleware to Guzzle's handler.

$logger = new \ColinODell\PsrTestLogger\TestLogger();
$handlerStack = \GuzzleHttp\HandlerStack::create();
$options = [];
$handlerStack->push(new \Loguzz\Middleware\LogMiddleware($logger, $options), 'logger');
  • $logger is the implementation of Psr\Log\LoggerInterface.
  • $options is an array to change the default behaviour of LogMiddleware.
  • 'logger' is the internal name of the middleware for Guzzle. It can be any name.

Options

// Default values
$options = [
    'length' => 100,
    'log_request' => true,
    'log_response' => true,
    'success_only' => false,
    'exceptions_only' => false,
    'log_level' => 'debug',
    'request_formatter' => new \Loguzz\Formatter\RequestCurlFormatter(),
    'response_formatter' => new \Loguzz\Formatter\ResponseJsonFormatter(),
    'exception_formatter' => new \Loguzz\Formatter\ExceptionJsonFormatter(),
    'tag' => '',
    'force_json' => true,
    'separate' => false,
];
  • length - int. Minimum 10. To set the length of when formatting request with \Loguzz\Formatter\RequestCurlFormatter.
  • log_request - bool. To enable or disable request logging.
  • log_response - bool. To enable or disable response logging.
  • success_only - bool. Only log successful responses. If the server could be reached, it's a success.
  • exception_only - bool Only log exceptions. Logs when an exception is thrown by Guzzle for connection/timeout related exceptions.
  • log_level - string. Any valid log level.
  • request_formatter - instance of \Loguzz\Formatter\AbstractRequestFormatter. Available
    • \Loguzz\Formatter\RequestArrayFormatter
    • \Loguzz\Formatter\RequestCurlFormatter
    • \Loguzz\Formatter\RequestJsonFormatter
  • response_formatter - instance of \Loguzz\Formatter\AbstractResponseFormatter
    • \Loguzz\Formatter\ResponseArrayFormatter
    • \Loguzz\Formatter\ResponseJsonFormatter
  • exception_formatter - instance of \Loguzz\Formatter\AbstractResponseFormatter
    • \Loguzz\Formatter\ExceptionArrayFormatter
    • \Loguzz\Formatter\ExceptionJsonFormatter
  • tag - string. Empty by default. When non-empty string, it'll log the formatted data under this tag. Tag can be used to search for specific type of request/response in your log file or your storage.
  • force_json - bool. true by default. It is only applicable when tag is non-empty string. If enabled, it will then log data as json string, otherwise it'll log as an array. If set to false, the code may break due to the type-hint in psr/log interface. If your logger interface supports array, it will work.
  • separate - bool. It is only applicable when tag is non-empty string. If enabled, it will then log data in {tag}.request, {tag}.success, {tag}.failure for request logging, successful response and error response.

Request Formatter

To create a new request formatter you need to extend the \Loguzz\Formatter\AbstractRequestFormatter class.

Response Formatter

To create a new response formatter you need to extend the \Loguzz\Formatter\AbstractResponseFormatter class.

Exception Formatter

To create a new exception formatter you need to extend the \Loguzz\Formatter\AbstractExceptionFormatter class.

Manual Request formatting

Implementations of \Loguzz\Formatter\AbstractRequestFormatter::format accept parameters as

  • \Psr\Http\Message\RequestInterface $request
  • array $options = []

Available request formatters parse data from $request and cookies from the $options. The values in $options['cookies'] must be an implementation of \GuzzleHttp\Cookie\CookieJarInterface. To parse cookies, the request URL must contain the domain.

Manual Response formatting

Implementations of \Loguzz\Formatter\AbstractResponseFormatter::format accept parameters as

  • \Psr\Http\Message\RequestInterface $request
  • \Psr\Http\Message\ResponseInterface $response
  • array $options = []

Available response formatters parse data from $response and cookies from the set-cookie header. To parse cookies, the request URL must contain the domain.

The set-cookie headers will not be available in the headers for available response formatters.

Issues & PRs

If you think something is missing in the package or cause bug, please report an Issue. If you're available and want to contribute to the repository, please submit a PR.

loguzz's People

Contributors

ls5302 avatar msvrtan avatar ssi-anik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

loguzz's Issues

Update for Guzzle 7

Hi,

Your code looks to work with Guzzle 7, but composer complains - I think it just needs a tweak in the composer.json:

    "guzzlehttp/guzzle": ">=6.2",

Thanks,

Lukasz

Class 'GuzzleHttp\Promise\Create' not found

Here's the exception we received in the production server.

{"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'GuzzleHttp\Promise\Create' not found at /prodPath/vendor/anik/loguzz/src/Middleware/LogMiddleware.php:191)

Additional screenshot:
image

Any idea on how to resolve this?

Guzzle ver. 7.*

Hello. Will you make changes in the code for Guzzle version 7.

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.