Created
October 23, 2014 07:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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