Skip to content

Commit

Permalink
Preparing for v0.3
Browse files Browse the repository at this point in the history
- Bumping PHP requirement to 5.6
- Adding some additional logging points
- Adding additional logging methods
  • Loading branch information
dcarbone committed Jul 21, 2016
1 parent 61f5d68 commit 398589e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],

"require": {
"php": ">=5.4.0",
"php": ">=5.6.0",
"dcarbone/curl-header-extractor": "2.0.*"
},

Expand Down
8 changes: 6 additions & 2 deletions src/AbstractApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
16 changes: 16 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -192,16 +192,15 @@ public function execute()
get_class($this),
json_encode($this->_curlOpts)
));
Logger::log('error', $err);
Logger::error($err);
return [null, $err];
}

$response = new HttpResponse(curl_exec($ch), curl_getinfo($ch), curl_error($ch));

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];
}
Expand Down
53 changes: 49 additions & 4 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();

0 comments on commit 398589e

Please sign in to comment.