-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
91 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
* @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 <[email protected]> | ||
* @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 <[email protected]> | ||
* @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 <[email protected]> | ||
* @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 <[email protected]> | ||
* @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']; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
"email": "[email protected]" | ||
} | ||
], | ||
"require": {}, | ||
"require": { | ||
"php-curl": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"omcrn\\unitedlogs\\": "" | ||
|