From 8a4858f5dc7d26f2acdd4d8e45cb1fc102b4660f Mon Sep 17 00:00:00 2001 From: Rodrigo Gonzalez Date: Fri, 4 Sep 2020 12:02:04 -0300 Subject: [PATCH] Log requests to console --- composer.json | 4 +++- src/Appsflyer.php | 13 +++++++------ src/Config.php | 28 +++++++++++++++++++++++++++- src/ConfigInterface.php | 10 ++++++++++ src/Core/Api.php | 12 +++++++++++- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index a7dc0c1..e2bc4e1 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ ], "require": { "php": ">=5.6", - "guzzlehttp/guzzle": "~6.0" + "guzzlehttp/guzzle": "~6.0", + "monolog/monolog": "^2.1", + "gmponos/guzzle_logger": "^2.0" }, "autoload": { "psr-4": { "Jlorente\\Appsflyer\\": "src/" } diff --git a/src/Appsflyer.php b/src/Appsflyer.php index 4b88188..a1a5864 100644 --- a/src/Appsflyer.php +++ b/src/Appsflyer.php @@ -52,9 +52,9 @@ class Appsflyer * @param string $apiToken * @return void */ - public function __construct($devKey = null, $apiToken = null) + public function __construct($devKey = null, $apiToken = null, $logRequests = false) { - $this->config = new Config(self::VERSION, $devKey, $apiToken); + $this->config = new Config(self::VERSION, $devKey, $apiToken, $logRequests); } /** @@ -62,11 +62,12 @@ class Appsflyer * * @param string $devKey * @param string $apiToken + * @param bool $logRequests * @return \Jlorente\Appsflyer\Appsflyer */ - public static function make($devKey = null, $apiToken = null) + public static function make($devKey = null, $apiToken = null, $logRequests = false) { - return new static($devKey, $apiToken); + return new static($devKey, $apiToken, $logRequests); } /** @@ -118,9 +119,9 @@ class Appsflyer * @param string $devKey * @return $this */ - public function setApiKey($devKey) + public function setDevKey($devKey) { - $this->config->setApiKey($devKey); + $this->config->setDevKey($devKey); return $this; } diff --git a/src/Config.php b/src/Config.php index 3d54229..e007d20 100644 --- a/src/Config.php +++ b/src/Config.php @@ -50,6 +50,13 @@ class Config implements ConfigInterface */ protected $accountId; + /** + * Define if we log requests or not + * + * @var bool + */ + protected $logRequests; + /** * Constructor. * @@ -59,7 +66,7 @@ class Config implements ConfigInterface * @return void * @throws \RuntimeException */ - public function __construct($version, $devKey, $apiToken) + public function __construct($version, $devKey, $apiToken, $logRequests = false) { $this->setVersion($version); @@ -74,6 +81,8 @@ class Config implements ConfigInterface if (!$this->devKey) { throw new \RuntimeException('The Appsflyer dev_key is not defined!'); } + + $this->setLogRequests($logRequests); } /** @@ -130,4 +139,21 @@ class Config implements ConfigInterface return $this; } + /** + * {@inheritdoc} + */ + public function getLogRequests() + { + return $this->logRequests; + } + + /** + * {@inheritdoc} + */ + public function setLogRequests($logRequests) + { + $this->logRequests = $logRequests; + + return $this; + } } diff --git a/src/ConfigInterface.php b/src/ConfigInterface.php index 40fa3bc..e24f4a7 100644 --- a/src/ConfigInterface.php +++ b/src/ConfigInterface.php @@ -66,4 +66,14 @@ interface ConfigInterface * @return $this */ public function setApiToken($apiVersion); + + /** + * Sets the property to log or not the requests + */ + public function setLogRequests($logRequests); + + /** + * Gets the property to log or not the requests + */ + public function getLogRequests(); } diff --git a/src/Core/Api.php b/src/Core/Api.php index 4a68e8d..1f6d737 100644 --- a/src/Core/Api.php +++ b/src/Core/Api.php @@ -33,6 +33,10 @@ use GuzzleHttp\Exception\ConnectException; use GuzzleHttp\Exception\TransferException; use GuzzleHttp\RequestOptions; +use GuzzleLogMiddleware\LogMiddleware; +use Monolog\Logger; +use Monolog\Handler\StreamHandler; + abstract class Api implements ApiInterface { @@ -197,7 +201,13 @@ abstract class Api implements ApiInterface return (int) pow(2, $retries) * 1000; })); + if ($this->config->getLogRequests()) { + $logger = new Logger('stderr'); + $logger->pushHandler(new StreamHandler('php://stderr')); + + $stack->push(new LogMiddleware($logger)); + } + return $stack; } - }