Skip to content
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

php sdk更新 #8

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

php sdk更新 #8

wants to merge 19 commits into from

Conversation

Fnck
Copy link
Contributor

@Fnck Fnck commented Dec 20, 2017

1、把request、response相关类移到对应文件夹,保持目录清爽
2、增加logger方式调用以及loglevel枚举
3、问题修复,自测通过

@Fnck
Copy link
Contributor Author

Fnck commented Dec 20, 2017

php 7.1.7测试通过

$request = new Aliyun_Log_Models_PutLogsRequest($this->project, $this->logstore,
$topic, $ip, $logItems);
$response = $this->client->putLogs($request);
print($response ->getRequestId());
Copy link
Collaborator

Choose a reason for hiding this comment

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

去掉print

@@ -39,9 +39,23 @@ public function log($logLevel, $logMessage, $topic){
$response = $this->client->putLogs($request);
print($response ->getRequestId());
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
var_dump($ex);
Copy link
Collaborator

Choose a reason for hiding this comment

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

所有的exception 都直接throw 出去,不要dump 下来


public function log($logMessage, $logLevel){
$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.

时间不需要了,默认已经有了

$logItem = new Aliyun_Log_Models_LogItem();
$logItem->setTime(time());
$logItem->setContents($contents);
printf($logMessage.'<br>');
Copy link
Collaborator

Choose a reason for hiding this comment

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

去掉print

$logItem = new Aliyun_Log_Models_LogItem();
$logItem->setTime(time());
$logItem->setContents($contents);
printf($logMessage.'<br>');
Copy link
Collaborator

Choose a reason for hiding this comment

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

去掉

$this->arraySize = 5;
}
$this->logger = $logger;
if($sem_id == null || $shm_id == null){
Copy link
Collaborator

Choose a reason for hiding this comment

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

如果sem_id , shm_id 等不为null的情况,会怎么样?

}

public function log($logMessage, $logLevel){
$prevoidCallTime = $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.

这个更新逻辑有问题, 只有发送的时候,才真正更新

$SEMKEY = 22 + time();
$SHMKEY = 33 + time();

$this->sem_id = sem_get($SEMKEY, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

在这边是否会遇到都线程同时操作这个logger对象的问题(这个我不是和确定),如果没有多线程问题,可以不加锁

}
shm_put_var($this->shm_id, 1, $this->logItems);

}

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));

@CLAassistant
Copy link

CLAassistant commented Jan 16, 2018

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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)。

$logItemTemps = array();
foreach ($logMessage as &$logElement){
$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 字段不需要了,下面有

$prevoisCallTime = 0;
$previousCallTime = $this->previousLogTime;
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。

$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($logstore === null || $logstore == ''){
throw new Exception('logstore name is blank!');
}
if($topic === null){
$topic = 'MainFlow';
Copy link
Collaborator

Choose a reason for hiding this comment

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

if topic == NULL => topic = ""

}
return static::$instanceSimpleLogger;
return static::$loggerMap[$loggerKey];
}

protected function __construct()
Copy link
Collaborator

Choose a reason for hiding this comment

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

析构的时候, 检查一下每个logger 中,是否还要没有发送出去的数据,如果有的话,需要发送出去

@@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

previousCallTime 只有每次发送之后,才更新,不是每次调用log 就更新

@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

不是每次都发送, 每次都是buffer 到cahce中。

$logItems = [];
}
$this->logItems = $logItems;
}
}

public function logSingleMessage(Aliyun_Log_Models_LogLevel_LogLevel $logLevel, $logMessage){
Copy link
Collaborator

Choose a reason for hiding this comment

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

logSingleMessage 和 logArrayMessage 如果保留的话, 前面那个log 函数就删除掉。

这两个函数都是把数据保存到cache中

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants