Skip to content
This repository was archived by the owner on Sep 6, 2019. It is now read-only.

Commit a9f53a5

Browse files
committed
added guzzle log subscriber
1 parent 3c8b966 commit a9f53a5

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"php": ">=5.4.0",
1414
"guzzlehttp/guzzle": "5.*",
1515
"guzzlehttp/retry-subscriber": "2.*",
16+
"guzzlehttp/log-subscriber": "1.0.*",
1617
"illuminate/support": "~4|~5"
1718
},
1819
"require-dev": {

src/Requester.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PulkitJalan\Requester;
44

55
use GuzzleHttp\Client as GuzzleClient;
6+
use GuzzleHttp\Subscriber\Log\Formatter;
7+
use GuzzleHttp\Subscriber\Log\LogSubscriber;
68
use GuzzleHttp\Subscriber\Retry\RetrySubscriber;
79
use PulkitJalan\Requester\Exceptions\InvalidUrlException;
810

@@ -103,6 +105,23 @@ public function getGuzzleClient()
103105
return $guzzle;
104106
}
105107

108+
/**
109+
* Add a logger to the guzzle client
110+
*
111+
* @param Logger $logger PSR-3 Logger instance (monolog)
112+
* @param string $format Log output format
113+
* @return void
114+
*/
115+
public function addLogger($logger, $format)
116+
{
117+
if (defined(Formatter::class.'::'.$format)) {
118+
$format = constant(Formatter::class.'::'.$format);
119+
}
120+
121+
$subscriber = new LogSubscriber($logger, $format);
122+
$this->guzzleClient->getEmitter()->attach($subscriber);
123+
}
124+
106125
/**
107126
* Set the url
108127
* will automatically append the protocol

src/RequesterServiceProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace PulkitJalan\Requester;
44

5-
use Illuminate\Support\ServiceProvider;
5+
use Monolog\Logger;
6+
use Monolog\Handler\StreamHandler;
67
use GuzzleHttp\Client as GuzzleClient;
8+
use Illuminate\Support\ServiceProvider;
79

810
class RequesterServiceProvider extends ServiceProvider
911
{
@@ -22,6 +24,16 @@ public function boot()
2224
$this->app[Requester::class] = function ($app) {
2325
return $app['requester'];
2426
};
27+
28+
if ($this->app->config->get('requester::log.enabled')) {
29+
$logger = $this->app->log->getMonolog();
30+
31+
if (!empty($this->app->config->get('requester::log.file'))) {
32+
$logger->pushHandler(new StreamHandler($this->app->config->get('requester::log.file'), Logger::INFO));
33+
}
34+
35+
$this->app['requester']->addLogger($logger, $this->app->config->get('requester::log.format'));
36+
}
2537
}
2638

2739
/**

src/config/config.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,47 @@
7272
|
7373
*/
7474
'on' => [500, 502, 503, 504],
75-
]
75+
],
76+
77+
/*
78+
|--------------------------------------------------------------------------
79+
| Logging options
80+
|--------------------------------------------------------------------------
81+
|
82+
| Log guzzle requests using the log-subscriber
83+
| https://github.com/guzzle/log-subscriber
84+
|
85+
*/
86+
'log' => [
87+
/*
88+
|--------------------------------------------------------------------------
89+
| Enable logging
90+
|--------------------------------------------------------------------------
91+
*/
92+
'enabled' => false,
93+
94+
/*
95+
|--------------------------------------------------------------------------
96+
| Logging file
97+
|--------------------------------------------------------------------------
98+
|
99+
| Logs to Laravels default (storage_path().'/logs/laravel.log')
100+
| Modify to log to a different file
101+
|
102+
*/
103+
'file' => '',
104+
105+
/*
106+
|--------------------------------------------------------------------------
107+
| Logging format
108+
|--------------------------------------------------------------------------
109+
|
110+
| The Log Subscriber currently supports [CLF, DEBUG, SHORT]
111+
| See https://github.com/guzzle/log-subscriber/blob/master/src/Formatter.php
112+
| Can also set a custom format
113+
|
114+
*/
115+
'format' => 'CLF',
116+
],
76117

77118
];

0 commit comments

Comments
 (0)