-
Notifications
You must be signed in to change notification settings - Fork 56
php sdk更新 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
php sdk更新 #8
Changes from 7 commits
8b0a8cb
3c28da2
91d5975
28a4487
45cedf2
cab78ce
b5b3546
cd00f2a
3535684
6c0e7ff
ef4f26d
ec57d5f
67f88e2
30ac7b1
faa0341
4215f3f
9232ddb
27208ab
c950d80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Copyright (C) Alibaba Cloud Computing | ||
* All rights reserved | ||
*/ | ||
|
||
class Aliyun_Log_Logger{ | ||
|
||
protected $client; | ||
|
||
protected $project; | ||
|
||
protected $logstore; | ||
|
||
public function __construct($client, $project, $logstore) | ||
{ | ||
$this ->client = $client; | ||
$this->logstore=$logstore; | ||
$this->project=$project; | ||
} | ||
|
||
public function log($logLevel, $logMessage, $topic){ | ||
if(!Aliyun_Log_Models_logLevel_LogLevel::isValidValue($logLevel)){ | ||
throw new Exception('logLevel value is invalid!'); | ||
} | ||
$ip = $this->getLocalIp(); | ||
$contents = array( // key-value pair | ||
'time'=>date('m/d/Y h:i:s a', time()), | ||
'message'=> $logMessage, | ||
'loglevel'=> $logLevel | ||
); | ||
try { | ||
$logItem = new Aliyun_Log_Models_LogItem(); | ||
$logItem->setTime(time()); | ||
$logItem->setContents($contents); | ||
$logitems = array($logItem); | ||
$request = new Aliyun_Log_Models_PutLogsRequest($this->project, $this->logstore, | ||
$topic, $ip, $logitems); | ||
$response = $this->client->putLogs($request); | ||
} catch (Aliyun_Log_Exception $ex) { | ||
var_dump($ex); | ||
} catch (Exception $ex) { | ||
var_dump($ex); | ||
} | ||
} | ||
|
||
public function logBatch($logItems, $topic){ | ||
$ip = $this->getLocalIp(); | ||
try{ | ||
$request = new Aliyun_Log_Models_PutLogsRequest($this->project, $this->logstore, | ||
$topic, $ip, $logItems); | ||
$response = $this->client->putLogs($request); | ||
} catch (Aliyun_Log_Exception $ex) { | ||
var_dump($ex); | ||
} catch (Exception $ex) { | ||
var_dump($ex); | ||
} | ||
} | ||
|
||
private function getLocalIp(){ | ||
$local_ip = getHostByName(php_uname('n')); | ||
if(strlen($local_ip) == 0){ | ||
$local_ip = getHostByName(getHostName()); | ||
} | ||
return $local_ip; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
/** | ||
* Copyright (C) Alibaba Cloud Computing | ||
* All rights reserved | ||
*/ | ||
|
||
/** | ||
* Class Aliyun_Log_Models_LogBatch | ||
* in some cases the http port is quite limited, so user could config a batch logger, | ||
* which will cache some log and send to server in bulk | ||
*/ | ||
class Aliyun_Log_Models_LogBatch{ | ||
|
||
private $logItems = []; | ||
|
||
private $arraySize; | ||
|
||
private $logger; | ||
|
||
private $sem_id; | ||
|
||
private $shm_id; | ||
|
||
private $topic; | ||
|
||
private $waitTime; | ||
|
||
private $previousLogTime; | ||
|
||
/** | ||
* Aliyun_Log_Models_LogBatch constructor. | ||
* @param Aliyun_Log_Logger $logger | ||
* @param $topic | ||
* @param null $cacheLogCount max log items limitation, by default it's 100 | ||
* @param null $cacheLogWaitTime max thread waiting time, bydefault it's 5 seconds | ||
*/ | ||
public function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount = null, $cacheLogWaitTime = null) | ||
{ | ||
if(NULL === $cacheLogCount || !is_integer($cacheLogCount)){ | ||
$this->arraySize = 100; | ||
}else{ | ||
$this->arraySize = $cacheLogCount; | ||
} | ||
|
||
if(NULL === $cacheLogWaitTime || !is_integer($cacheLogWaitTime)){ | ||
$this->waitTime = 5; | ||
}else{ | ||
$this->waitTime = $cacheLogWaitTime; | ||
} | ||
|
||
$this->logger = $logger; | ||
$this->topic = $topic; | ||
|
||
$time_stampe = time(); | ||
$MEMSIZE = 5120; | ||
$SEMKEY = $time_stampe; | ||
$SHMKEY = $time_stampe+2233; | ||
|
||
$this->sem_id = sem_get($SEMKEY, 10); // support 10 processes run simultaneously | ||
$this->shm_id = shm_attach($SHMKEY, $MEMSIZE); | ||
if(shm_has_var($this->shm_id, 1)){ | ||
shm_remove_var($this->shm_id, 1); | ||
} | ||
shm_put_var($this->shm_id, 1, $this->logItems); | ||
|
||
} | ||
|
||
/** | ||
* log expected message with proper level | ||
* @param $logMessage | ||
* @param $logLevel | ||
*/ | ||
public function log($logMessage, $logLevel){ | ||
|
||
$previousCallTime = $this->previousLogTime; | ||
|
||
if(NULL === $previousCallTime){ | ||
$previousCallTime = 0; | ||
|
||
} | ||
$this->previousLogTime = time(); | ||
if(is_array($logMessage)){ | ||
$logItemTemps = array(); | ||
foreach ($logMessage as &$logElement){ | ||
|
||
$contents = array( // key-value pair | ||
'time'=>date('m/d/Y h:i:s a', time()), | ||
|
||
'message'=> $logElement, | ||
'loglevel'=> $logLevel | ||
); | ||
$logItem = new Aliyun_Log_Models_LogItem(); | ||
$logItem->setTime(time()); | ||
$logItem->setContents($contents); | ||
array_push($logItemTemps, $logItem); | ||
|
||
|
||
} | ||
$this->logger->logBatch($logItemTemps, $this->topic); | ||
}else{ | ||
$contents = array( // key-value pair | ||
'time'=>date('m/d/Y h:i:s a', time()), | ||
'message'=> $logMessage, | ||
'loglevel'=> $logLevel | ||
); | ||
$logItem = new Aliyun_Log_Models_LogItem(); | ||
$logItem->setTime(time()); | ||
$logItem->setContents($contents); | ||
if(shm_has_var($this->shm_id, 1)){ | ||
$logItems = shm_get_var($this->shm_id, 1); | ||
array_push($logItems, $logItem); | ||
|
||
if((sizeof($logItems) == $this->arraySize || $this->previousLogTime - $previousCallTime > 5000) | ||
&& $previousCallTime > 0){ | ||
$this->logger->logBatch($logItems, $this->topic); | ||
$logItems = []; | ||
} | ||
|
||
shm_remove_var($this->shm_id, 1); | ||
shm_put_var($this->shm_id, 1, $logItems); | ||
$this->logItems = $logItems; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* manually flush all cached log to log server | ||
*/ | ||
public function logFlush(){ | ||
if(sizeof($this->logItems) > 0){ | ||
$this->logger->logBatch($this->logItems, $this->topic); | ||
$this->logItems = []; | ||
} | ||
shm_remove_var($this->shm_id, 1); | ||
} | ||
|
||
function __destruct() { | ||
if(sizeof($this->logItems) > 0){ | ||
$this->logger->logBatch($this->logItems, $this->topic); | ||
} | ||
|
||
sem_remove($this->sem_id); | ||
shm_remove($this->shm_id); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所有的exception 都直接throw 出去,不要dump 下来