Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions Aliyun/Log/Models/LogBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,50 @@ public function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount =
* @param $logLevel
*/
public function log($logMessage, $logLevel){
Copy link
Collaborator

@suntingtao007 suntingtao007 Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于log的话,单个message 有点太简单了,也可以直接同时写入多个key:value的模式 , 单个message 是一种特例。
可以提供一个log(level, array)的接口 , log(level, message) => log (leve , array("msg"=>message));

$prevoisCallTime = $this->previousLogTime;
if(NULL === $prevoisCallTime){
$prevoisCallTime = 0;
$previousCallTime = $this->previousLogTime;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了易用,这个log 接口,可以再加一个bool的参数, use_buffer (默认为true),如果设置为false, 这立刻flush出去

if(NULL === $previousCallTime){
$previousCallTime = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个时间的判断逻辑有问题,修改如下:

  1. previousCallTime = time() 初始的时候
  2. 每次调用一次log, 检查 time() - previousCallTime > xxx 是否满足
  3. 如果满足 send data, 并且更新previousCallTime = time()
  4. 如果第二不检查不满足, 则不send data, 也不更新 previousCallTime。

}
$this->previousLogTime = time();
$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);
printf($logMessage.'<br>');
if(shm_has_var($this->shm_id, 1)){
$logItems = shm_get_var($this->shm_id, 1);
array_push($logItems, $logItem);
if(is_array($logMessage)){
$logItemTemps = array();
foreach ($logMessage as &$logElement){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当这个作为一个array的时候, 是一条message, 而不是多条,一条message 可以有多个key,value。
传入的是array(其实是一个map)。

$contents = array( // key-value pair
'time'=>date('m/d/Y h:i:s a', time()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边的time 字段不需要了,下面有

'message'=> $logElement,
'loglevel'=> $logLevel
);
$logItem = new Aliyun_Log_Models_LogItem();
$logItem->setTime(time());
$logItem->setContents($contents);
array_push($logItemTemps, $logItem);

if((sizeof($logItems) == $this->arraySize || $this->previousLogTime - $prevoisCallTime > 5000)
&& $prevoisCallTime > 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;
}
$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;
}
}
}

Expand Down