From 2ae275432af20d89942a0543587955e163a81f9f Mon Sep 17 00:00:00 2001 From: Zura Sekhniashvili Date: Tue, 23 May 2017 15:33:22 +0400 Subject: [PATCH] Rewrite to curl. Improve code and documentations --- Log.php | 132 +++++++++++++++++++++++++++++++++----------------- composer.json | 4 +- 2 files changed, 91 insertions(+), 45 deletions(-) diff --git a/Log.php b/Log.php index 02f39ef..5674869 100644 --- a/Log.php +++ b/Log.php @@ -12,24 +12,30 @@ */ class Log { - const DOMAIN_KEY_IN_ENV = 'UNITED_LOGS_DOMAIN'; + private $levels = array('warning', 'info', 'error', 'success'); private $key; private $environment; private $domain; + const LEVEL_WARNING = 'warning'; + const LEVEL_ERROR = 'error'; + const LEVEL_INFO = 'info'; + const LEVEL_SUCCESS = 'success'; + /** - * Log constructor. - * takes $key generated in the united-logs, $environment for better filtering different modes: development, testing, production - * optional $domain. if you have united-logs on your server, please specify domain or ip. Else default domain will be used. + * Log constructor. Initializes logger object. + * * - * @param $key string - * @param $environment string - * @param $domain string|null + * @param string $key Project key. 64 bit length string associated to project + * @param string $environment For better filtering different modes: development, testing, production. You can put any text you want + * @param array|null $levels + * @param string|null $domain The endpoint of United Logs server. If you have it on your server, please specify domain or ip. + * You can put the domain in $_ENV['UNITED_LOGS_DOMAIN'] and not specify here * @throws Exception */ - public function __construct($key, $environment, $domain = null) + public function __construct($key, $environment, $levels = null, $domain = null) { if (!$key) { throw new Exception('API Key not specified'); @@ -39,64 +45,96 @@ public function __construct($key, $environment, $domain = null) } $this->key = $key; $this->environment = $environment; + + if ($levels !== null){ + $this->levels = $levels; + } + if ($domain === null && !isset($_ENV[self::DOMAIN_KEY_IN_ENV])){ - throw new Exception('Please put the domain in global environment variable with key "'.self::DOMAIN_KEY_IN_ENV.'" or specify it when constructing '.static::class.' object.'); + throw new Exception('Please put the domain in global $_ENV variable with key "'.self::DOMAIN_KEY_IN_ENV.'" or specify it when constructing '.get_class($this).' object.'); } $this->domain = $domain ? ($domain . '/api/v1/log') : $_ENV[self::DOMAIN_KEY_IN_ENV].'/api/v1/log'; } /** - * @param $message string - * @param $category string - * @param $params string|null + * Write error log. It accepts message, category and optional params. + * Return whether or not the log has been successfully written. + * + * @author Zura Sekhniashvili + * @param string $message + * @param string $category + * @param array|null $params + * @return bool */ public function error($message, $category, $params = null) { - $this->sendLog($this->domain . '/error', $message, $category, $params); + return $this->sendLog(self::LEVEL_ERROR, $message, $category, $params); } /** - * @param $message string - * @param $category string - * @param $params string|null + * Write success log. It accepts message, category and optional params. + * Return whether or not the log has been successfully written. + * + * @author Zura Sekhniashvili + * @param string $message + * @param string $category + * @param array|null $params + * @return bool */ public function success($message, $category, $params = null) { - $this->sendLog($this->domain . '/success', $message, $category, $params); + return $this->sendLog(self::LEVEL_SUCCESS, $message, $category, $params); } /** - * @param $message string - * @param $category string - * @param $params string|null + * Write info log. It accepts message, category and optional params. + * Return whether or not the log has been successfully written. + * + * @author Zura Sekhniashvili + * @param string $message + * @param string $category + * @param array|null $params + * @return bool */ public function info($message, $category, $params = null) { - $this->sendLog($this->domain . '/info', $message, $category, $params); + return $this->sendLog(self::LEVEL_INFO, $message, $category, $params); } /** - * @param $message string - * @param $category string - * @param $params string|null + * Write warning log. It accepts message, category and optional params. + * Return whether or not the log has been successfully written. + * + * @author Zura Sekhniashvili + * @param string $message + * @param string $category + * @param array|null $params + * @return bool */ public function warning($message, $category, $params = null) { - $this->sendLog($this->domain . '/warning', $message, $category, $params); + return $this->sendLog(self::LEVEL_WARNING, $message, $category, $params); } - /** - * @param $url - * @param $message - * @param $category - * @param $params + * Send log message + * + * @author Zura Sekhniashvili + * @param string $level + * @param string $message + * @param string $category + * @param array $params + * @return bool */ - private function sendLog($url, $message, $category, $params) + private function sendLog($level, $message, $category, $params) { + if (!in_array($level, array(self::LEVEL_WARNING, self::LEVEL_INFO, self::LEVEL_SUCCESS, self::LEVEL_ERROR))){ + return false; + } + $data = array( 'api' => $this->key, 'environment' => $this->environment, @@ -105,19 +143,25 @@ private function sendLog($url, $message, $category, $params) 'params' => $params ); - $options = array( - 'http' => array( - 'header' => "Content-type: application/x-www-form-urlencoded\r\n", - 'method' => 'POST', - 'content' => http_build_query($data) - ) - ); - $context = stream_context_create($options); - $result = file_get_contents($url, false, $context); - if ($result === FALSE) { - /* Handle error */ - } - var_dump($result); + // create a new cURL resource + $ch = curl_init(); + + // set URL and other appropriate options + curl_setopt($ch, CURLOPT_URL, $this->domain.'/'.$level); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($ch, CURLOPT_HEADER, "Content-type: application/x-www-form-urlencoded"); + + // receive server response ... + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $server_output = curl_exec ($ch); + + // close cURL resource, and free up system resources + curl_close($ch); + + $result = json_decode($server_output, true); + return $result['success']; } } \ No newline at end of file diff --git a/composer.json b/composer.json index 031181d..7683878 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,9 @@ "email": "zurasekhniashvili@gmail.com" } ], - "require": {}, + "require": { + "php-curl": "*" + }, "autoload": { "psr-4": { "omcrn\\unitedlogs\\": ""