-
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 1 commit
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
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,26 +4,29 @@ | |
| * All rights reserved | ||
| */ | ||
|
|
||
| class Aliyun_Log_LoggerFactory extends Aliyun_Log_SimpleLogger{ | ||
| protected static $instanceLogger = null; | ||
| protected static $instanceSimpleLogger = null; | ||
| /** | ||
| * Class Aliyun_Log_LoggerFactory | ||
| */ | ||
| class Aliyun_Log_LoggerFactory{ | ||
|
|
||
| public static function getLogger($client, $project, $logstore){ | ||
| if (!isset(static::$instanceLogger)) { | ||
| static::$instanceLogger = new Aliyun_Log_Logger($client, $project, $logstore); | ||
| } | ||
| return static::$instanceLogger; | ||
| } | ||
| private static $loggerMap = array(); | ||
|
|
||
| public static function getSimpleLogger($client, $project, $logstore, $topic=null){ | ||
| public static function getLogger($client, $project, $logstore, $topic = null){ | ||
| if($project === null || $project == ''){ | ||
| throw new Exception('project name is blank!'); | ||
| } | ||
| if($logstore === null || $logstore == ''){ | ||
| throw new Exception('logstore name is blank!'); | ||
| } | ||
| if($topic === null){ | ||
| $topic = 'MainFlow'; | ||
| } | ||
| if (!isset(static::$instanceSimpleLogger)) { | ||
| $logger = new Aliyun_Log_Logger($client, $project, $logstore); | ||
| static::$instanceSimpleLogger = new Aliyun_Log_SimpleLogger($logger,$topic); | ||
| $loggerKey = $project.'#'.$logstore.'#'.$topic; | ||
| if (!array_key_exists($loggerKey, static::$loggerMap) || static::$loggerMap[$loggerKey] === null) { | ||
| $instanceSimpleLogger = new Aliyun_Log_SimpleLogger($client,$project,$logstore,$topic); | ||
| static::$loggerMap[$loggerKey] = $instanceSimpleLogger; | ||
| } | ||
| return static::$instanceSimpleLogger; | ||
| return static::$loggerMap[$loggerKey]; | ||
| } | ||
|
|
||
| protected function __construct() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 析构的时候, 检查一下每个logger 中,是否还要没有发送出去的数据,如果有的话,需要发送出去 |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,32 +5,37 @@ | |
| */ | ||
|
|
||
| /** | ||
| * 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_SimpleLogger | ||
| * simple logger for submit log message | ||
| */ | ||
| class Aliyun_Log_SimpleLogger extends Aliyun_Log_Logger { | ||
| class Aliyun_Log_SimpleLogger{ | ||
|
|
||
| private $logItems = []; | ||
|
|
||
| private $arraySize; | ||
|
|
||
| private $logger; | ||
|
|
||
| private $topic; | ||
|
|
||
| private $waitTime; | ||
|
|
||
| private $previousLogTime; | ||
|
|
||
| private $client; | ||
|
|
||
| private $project; | ||
|
|
||
| private $logstore; | ||
|
|
||
| /** | ||
| * Aliyun_Log_Models_LogBatch constructor. | ||
| * @param Aliyun_Log_Logger $logger | ||
| * @param $client log client | ||
| * @param $project the corresponding project | ||
| * @param $logstore the logstore | ||
| * @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 | ||
| */ | ||
| protected function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount = null, $cacheLogWaitTime = null) | ||
| public function __construct($client, $project, $logstore, $topic, $cacheLogCount = null, $cacheLogWaitTime = null) | ||
| { | ||
| if(NULL === $cacheLogCount || !is_integer($cacheLogCount)){ | ||
| $this->arraySize = 10; | ||
|
|
@@ -43,8 +48,12 @@ protected function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount | |
| }else{ | ||
| $this->waitTime = $cacheLogWaitTime; | ||
| } | ||
|
|
||
| $this->logger = $logger; | ||
| if($client == null || $project == null || $logstore == null){ | ||
| throw new Exception('the input parameter is invalid! create SimpleLogger failed!'); | ||
| } | ||
| $this->client = $client; | ||
| $this->project = $project; | ||
| $this->logstore = $logstore; | ||
| $this->topic = $topic; | ||
| } | ||
|
|
||
|
|
@@ -54,7 +63,7 @@ protected function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount | |
| * @param $logLevel | ||
| * @param $topic should be null | ||
| */ | ||
| public function log(Aliyun_Log_Models_LogLevel_LogLevel $logLevel,$logMessage, $topic = null){ | ||
| public function log(Aliyun_Log_Models_LogLevel_LogLevel $logLevel,$logMessage){ | ||
| $previousCallTime = $this->previousLogTime; | ||
|
||
| if(null === $previousCallTime){ | ||
| $previousCallTime = 0; | ||
|
|
@@ -73,7 +82,7 @@ public function log(Aliyun_Log_Models_LogLevel_LogLevel $logLevel,$logMessage, $ | |
| $logItem->setContents($contents); | ||
| array_push($logItemTemps, $logItem); | ||
| } | ||
| $this->logger->logBatch($logItemTemps, $this->topic); | ||
| $this->logBatch($logItemTemps, $this->topic); | ||
|
||
| }else{ | ||
| $logItems = $this->logItems; | ||
| $contents = array( // key-value pair | ||
|
|
@@ -90,26 +99,106 @@ public function log(Aliyun_Log_Models_LogLevel_LogLevel $logLevel,$logMessage, $ | |
| if((sizeof($logItems) == $this->arraySize | ||
| || $this->previousLogTime - $previousCallTime > 5000) | ||
| && $previousCallTime > 0){ | ||
| $this->logger->logBatch($logItems, $this->topic); | ||
| $this->logBatch($logItems, $this->topic); | ||
| $logItems = []; | ||
| } | ||
| $this->logItems = $logItems; | ||
| } | ||
| } | ||
|
|
||
| public function logSingleMessage(Aliyun_Log_Models_LogLevel_LogLevel $logLevel, $logMessage){ | ||
|
||
| if(!$logLevel instanceof Aliyun_Log_Models_LogLevel_LogLevel){ | ||
| throw new Exception('LogLevel value is invalid!'); | ||
| } | ||
| if(is_array($logMessage)){ | ||
| throw new Exception('array is not supported in this function, please use logArrayMessage!'); | ||
| } | ||
| $ip = $this->getLocalIp(); | ||
| $contents = array( // key-value pair | ||
| 'time'=>date('m/d/Y h:i:s a', time()), | ||
| 'message'=> $logMessage, | ||
| 'loglevel'=> Aliyun_Log_Models_LogLevel_LogLevel::getLevelStr($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, | ||
| $this->topic, $ip, $logitems); | ||
| $response = $this->client->putLogs($request); | ||
| } catch (Aliyun_Log_Exception $ex) { | ||
| var_dump($ex); | ||
| } catch (Exception $ex) { | ||
| var_dump($ex); | ||
| } | ||
| } | ||
|
|
||
| public function logArrayMessage(Aliyun_Log_Models_LogLevel_LogLevel $logLevel, $logMessage){ | ||
| if(!$logLevel instanceof Aliyun_Log_Models_LogLevel_LogLevel){ | ||
| throw new Exception('LogLevel value is invalid!'); | ||
| } | ||
| if(!is_array($logMessage)){ | ||
| throw new Exception('input message is not array, please use logSingleMessage!'); | ||
| } | ||
| $contents = array( // key-value pair | ||
| 'time'=>date('m/d/Y h:i:s a', time()) | ||
| ); | ||
| $ip = $this->getLocalIp(); | ||
| if(is_array($logMessage)){ | ||
| foreach ($logMessage as $key => $value) | ||
| $contents[$key] = $value; | ||
| } | ||
| $contents['logLevel'] = Aliyun_Log_Models_LogLevel_LogLevel::getLevelStr($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, | ||
| $this->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; | ||
| } | ||
|
|
||
| private 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); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * manually flush all cached log to log server | ||
| */ | ||
| public function logFlush(){ | ||
| if(sizeof($this->logItems) > 0){ | ||
| $this->logger->logBatch($this->logItems, $this->topic); | ||
| $this->logBatch($this->logItems, $this->topic); | ||
| $this->logItems = []; | ||
| } | ||
| } | ||
|
|
||
| function __destruct() { | ||
| if(sizeof($this->logItems) > 0){ | ||
| $this->logger->logBatch($this->logItems, $this->topic); | ||
| $this->logBatch($this->logItems, $this->topic); | ||
| } | ||
| } | ||
| } | ||
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.
if topic == NULL => topic = ""