Download the PHP package dittto/symfony-request-logger without Composer
On this page you can find all versions of the php package dittto/symfony-request-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dittto/symfony-request-logger
More information about dittto/symfony-request-logger
Files in dittto/symfony-request-logger
Package symfony-request-logger
Short Description Handles logging external requests via guzzle
License MIT
Informations about the package symfony-request-logger
Symfony request logger
What is it?
Any apps we write will, quite often, have cause to make external calls to either our own, or third-party, APIs. This Symfony bundle makes it easy to track how long these requests are taking and return the result via either monolog and/or an additional JSON object that's appended to your output when Symfony's in debug mode.
It works by adding extra middleware to Guzzle so we can track when a request is made and either succeeds or fails, and then storing these logs for later output.
How to set it up
Basic setup
There are a few different options for using this request logger. The most simple is to log all requests made via Guzzle, and then recall the logs later.
To do this, add / update the following services. If you've already got a Guzzle service, then update it as required:
What does this do then? The first service is to define which version of the request logger we're going to use. dittto.request_logger
is the most basic form of LoggerInterface
that simply stores all requests for later.
After that, we create a Guzzle client, and then make sure our middleware has been added to it, so we can store our API request attempts.
To use this, you'd probably do something like:
With monolog
Above shows how to use it and deal with the result yourself. Realistically, you're going to want to automatically output the request logger one way or another.
We'll use monolog to output our logged messages this time. To begin with, let's create a unique monolog channel for all of these messages:
Next, create the following services. If you've already defined Guzzle then just pick and choose the settings you wish to use:
Note that the changes this time are to the first two services. The alias now references dittto.request_logger.passthrough
which allows us to save our logged messages to both our request logger, and monolog.
The second service is an alias to the new monolog channel we created above.
Now if you run your code, both monolog will save changes and the request logger has them available via getLogs()
.
If you're just going to use monolog for recording those messages, then you can use just the guzzle middleware instead of everything else. This is done using the following services:
JSON debug object
If you're building, maintaining, and debugging a lot of JSON APIs, then you can have this plugin automatically add your request logs to your JSON output when Symfony is in debug mode.
Add the following service to your app to capture valid responses and update your JSON: