Guzzle Subscriber for logging to debugbar's timeline and logs.
Just four lines of code are needed to log your requests to debugbar's timeline.
$debugBar = new StandardDebugBar();
// Get data collectors.
$timeline = $debugBar->getCollector('time');
$exceptions = $debugBar->getCollector('exceptions');
// Create the subscriber.
$subscriber = new GuzzleHttp\Subscriber\Log\DebugbarSubscriber($timeline, $exceptions);
// Attach it to the Guzzle client's event emitter.
$client = new GuzzleHttp\Client;
$client->getEmitter()->attach($subscriber);
Now $client
is ready to make requests.
Every request is now logged to the timeline, and thrown exceptions are logged to the 'Messages' tab.
$debugBar = new StandardDebugBar();
// PSR-3 logger:
$logger = $debugBar->getCollector('messages');
// Create a new LogSubscriber.
$subscriber = new GuzzleHttp\Subscriber\Log\LogSubscriber($logger);
// Attach it to the Guzzle client's event emitter.
$client = new GuzzleHttp\Client;
$client->getEmitter()->attach($subscriber);
It is recommended to have my friend Barry's laravel-debugbar configured.
Make sure to include the GuzzleHttp\Subscriber\Log\Support\Laravel\ServiceProvider
from this package your app's
providers
array.
If you just want to use a different implementation of DebugBar\DebugBar
, create a ServiceProvider that binds an
implementation with the key 'debugbar'
. For example with this register method:
public function register()
{
$this->app->singleton('debugbar', function () {
return new StandardDebugBar;
});
}
Make sure to create every Guzzle Client via injection or via App::make
. Pass on the config array the Guzzle Client's
constructor if needed.
public function __construct(\GuzzleHttp\Client $client) {
$this->client = $client;
$this->client->get('http://example.org/abolute/url');
}
public function __construct(Illuminate\Foundation\Application $app) {
$this->client = $app->make('\GuzzleHttp\Client', [['base_url' => 'https://api.github.com']]);
$this->client->get('/users/hannesvdvreken')->json();
}
'time' is not a registered collector
'exceptions' is not a registered collector
If you see one of these error messages, it means you disabled the time
collector and/or the exceptions
collector
in your packages/barryvdh/laravel-debugbar/config.php
. By default these are enabled. This package depends on it,
so please enable them both.
Please be nice in issues/pull request, and stick to PSR-2. Everything will be just fine.
Run tests with composer test
.