From 398589e6781c15ec6519a7e4287ff7d3cad96868 Mon Sep 17 00:00:00 2001 From: Daniel Paul Carbone Date: Thu, 21 Jul 2016 09:29:16 -0500 Subject: [PATCH] Preparing for v0.3 - Bumping PHP requirement to 5.6 - Adding some additional logging points - Adding additional logging methods --- composer.json | 2 +- src/AbstractApiClient.php | 8 ++++-- src/DateTime.php | 7 ++++++ src/Error.php | 16 ++++++++++++ src/HttpRequest.php | 7 +++--- src/Logger.php | 53 ++++++++++++++++++++++++++++++++++++--- 6 files changed, 82 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index e767e05..563f8cf 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { - "php": ">=5.4.0", + "php": ">=5.6.0", "dcarbone/curl-header-extractor": "2.0.*" }, diff --git a/src/AbstractApiClient.php b/src/AbstractApiClient.php index 7e647f4..49a8434 100644 --- a/src/AbstractApiClient.php +++ b/src/AbstractApiClient.php @@ -49,13 +49,17 @@ protected function requireOK(array $requestResult) if (200 !== $requestResult[1]->httpCode) { - return [$requestResult[0], $requestResult[1], new Error(sprintf( + $err = new Error(sprintf( '%s - Error seen while executing "%s". Response code: %d. Message: %s', get_class($this), $requestResult[1]->url, $requestResult[1]->httpCode, $requestResult[1]->curlError - ))]; + )); + + Logger::error($err); + + return [$requestResult[0], $requestResult[1], $err]; } return $requestResult; diff --git a/src/DateTime.php b/src/DateTime.php index b6e6abb..ba8c97a 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -22,6 +22,13 @@ */ class DateTime extends \DateTime implements \JsonSerializable { + const Nanosecond = 1; + const Microsecond = 1000 * self::Nanosecond; + const Millisecond = 1000 * self::Microsecond; + const Second = 1000 * self::Millisecond; + const Minute = 60 * self::Second; + const Hour = 60 * self::Minute; + /** @var string */ private static $_defaultFormat = 'Y-m-d\TH:i:s.uO'; diff --git a/src/Error.php b/src/Error.php index 8998545..d2894dc 100644 --- a/src/Error.php +++ b/src/Error.php @@ -40,6 +40,22 @@ public function __construct($message) $this->_message = $message; } + /** + * @return DateTime + */ + public function getTimestamp() + { + return $this->_timestamp; + } + + /** + * @return string + */ + public function getMessage() + { + return $this->_message; + } + /** * Specify data which should be serialized to JSON * @link http://php.net/manual/en/jsonserializable.jsonserialize.php diff --git a/src/HttpRequest.php b/src/HttpRequest.php index 3ef49b9..c90a1dd 100644 --- a/src/HttpRequest.php +++ b/src/HttpRequest.php @@ -150,7 +150,7 @@ public function execute() ); } - Logger::log('debug', 'Executing '.$this->method.' request '.$this->url.($this->body ? ' with body "'.$this->body.'"':'')); + Logger::debug('Executing '.$this->method.' request '.$this->url.($this->body ? ' with body "'.$this->body.'"':'')); switch($this->method) { @@ -192,7 +192,7 @@ public function execute() get_class($this), json_encode($this->_curlOpts) )); - Logger::log('error', $err); + Logger::error($err); return [null, $err]; } @@ -200,8 +200,7 @@ public function execute() curl_close($ch); - - Logger::log('debug', 'Response received - Code: '.$response->httpCode.'; Body: '.$response->body.'; '); + Logger::debug('Response received - Code: '.$response->httpCode.'; Body: '.$response->body.'; '); return [$response, null]; } diff --git a/src/Logger.php b/src/Logger.php index 31f6cc5..541b234 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -116,14 +116,27 @@ public static function resetLoggers() } /** - * @param string $logLevel + * @param string $level * @param string $message */ - public static function log($logLevel, $message) + public static function log($level, $message) { - $level = strtolower($logLevel); - if (self::$_logLevels[self::$_logLevel] <= self::$_logLevels[strtolower($level)]) + if (!is_string($level) || !isset(self::$_logLevels[$level])) + { + trigger_error(sprintf( + '%s::log - Specified level "%s" is not valid. Available levels are: ["%s"]', + get_called_class(), + is_string($level) ? $level : gettype($level), + implode('","', array_keys(self::$_logLevels)) + )); + return; + } + + if (self::$_logLevels[self::$_logLevel] <= self::$_logLevels[$level]) { + if ($message instanceof Error) + $message = $message->getMessage(); + if (self::$_defaultLogger) self::$_defaultLogger->{$level}($message); @@ -133,5 +146,37 @@ public static function log($logLevel, $message) } } } + + /** + * @param string $message + */ + public static function error($message) + { + self::log('error', $message); + } + + /** + * @param string $message + */ + public static function warn($message) + { + self::log('warn', $message); + } + + /** + * @param string $message + */ + public static function info($message) + { + self::log('info', $message); + } + + /** + * @param string $message + */ + public static function debug($message) + { + self::log('debug', $message); + } } Logger::init(); \ No newline at end of file