Skip to content

Instantly share code, notes, and snippets.

@klipach
Created October 23, 2014 07:01
Show Gist options
  • Save klipach/7a25263ae8d88dd47f8d to your computer and use it in GitHub Desktop.
Save klipach/7a25263ae8d88dd47f8d to your computer and use it in GitHub Desktop.
Using Guzzle4 as a service in Symfony 2.x project with logging via Monolog
# Sometimes is it is required to add logging of all guzzle requests into log file via Monolog
# You need to add https://github.com/guzzle/log-subscriber to your project
# app/config/config.yml
monolog:
channels: ["guzzle"]
handlers:
main:
channels: ["!guzzle"]
guzzle:
type: stream
path: "%kernel.logs_dir%/guzzle.log" # path to custom log file
channels: [guzzle]
# services.yml in your bundle
parameters:
acme.request.client.class: GuzzleHttp\Client
acme.request.emitter.class: GuzzleHttp\Event\Emitter
acme.guzzle.log.subscriber.class: GuzzleHttp\Subscriber\Log\LogSubscriber
services:
acme.request.client:
class: %acme.request.client.class%
arguments:
- { base_url: 'http://path.to.api', defaults: { headers: { Accept: "application/xml" } }, emitter: @acme.request.emitter }
acme.request.emitter:
class: %acme.request.emitter.class%
calls:
- [attach, [@acme.guzzle.log.subscriber]]
tags:
- { name: guzzle.client }
acme.guzzle.log.subscriber:
class: %acme.guzzle.log.subscriber.class%
arguments:
- @monolog.logger.guzzle
- ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}" # not mandatory parameter - log format, copy pasted from https://github.com/guzzle/log-subscriber/blob/master/src/Formatter.php#L39
tags:
- { name: guzzle.client }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment