Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions Aliyun/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 下来

} catch (Exception $ex) {
logVarDump($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);
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

} catch (Aliyun_Log_Exception $ex) {
var_dump($ex);
} catch (Exception $ex) {
var_dump($ex);
}
}

Expand Down
87 changes: 87 additions & 0 deletions Aliyun/Log/Models/LogBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/

class Aliyun_Log_Models_LogBatch{

private $logItems = [];

private $arraySize;

private $logger;

private $sem_id;

private $shm_id;

public function __construct($maxLogSize, Aliyun_Log_Logger $logger, $sem_id = null, $shm_id = null)
{
$this->arraySize = $maxLogSize;
if(!is_integer($maxLogSize)){
$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的情况,会怎么样?

$MEMSIZE = 10240;
$SEMKEY = 22;
$SHMKEY = 33;

$this->sem_id = sem_get($SEMKEY, 1);
sem_acquire($this->sem_id);
$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);
}
}

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

$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.

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

'message'=> $logMessage,
'loglevel'=> $logLevel
);
$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

Copy link
Collaborator

Choose a reason for hiding this comment

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

去掉

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->logger->logBatch($logItems, 'MainFlow');
$logItems = [];
}

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

public function shareSem($shm_id){

if(shm_has_var($shm_id, 1)){
$tmp = shm_get_var($shm_id, 1);
shm_put_var($shm_id, 1, "*,".$tmp);
}else{
shm_put_var($shm_id, 1, "Variable 1");
}

$result = shm_get_var($shm_id, 1);
echo $result."<br>";
}

function __destruct() {
if(sizeof($this->logItems) > 0){
$this->logger->logBatch($this->logItems, 'MainFlow');
}

sem_remove($this->sem_id);
shm_remove ($this->shm_id);
}
}
1 change: 0 additions & 1 deletion Aliyun/Log/Models/Response/ListShardsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Aliyun_Log_Models_ListShardsResponse extends Aliyun_Log_Models_Response {
public function __construct($resp, $header) {
parent::__construct ( $header );
foreach($resp as $key=>$value){
var_dump($value);
$this->shardIds[] = $value['shardID'];
$this->shards[] = new Aliyun_Log_Models_Shard($value['shardID'],$value["status"],$value["inclusiveBeginKey"],$value["exclusiveEndKey"],$value["createTime"]);
}
Expand Down
14 changes: 10 additions & 4 deletions sample/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function getLogs(Aliyun_Log_Client $client, $project, $logstore) {
}

$endpoint = 'http://cn-shanghai-corp.sls.aliyuncs.com';
$accessKeyId = 'LTAIUbY1Pk7Ryf1P';
$accessKey = '0oXZLJrFoRnlzVpDpopNVstd87bUWn';
$accessKeyId = '';
$accessKey = '';
$project = 'ali-sls-sdk-test';
$logstore = 'sls-test';
$token = "";
Expand All @@ -81,6 +81,12 @@ function getLogs(Aliyun_Log_Client $client, $project, $logstore) {

$logger = new Aliyun_Log_Logger($client, $project, $logstore);

$logger->log('test', 'something wrong with the inner info', 'MainFlow');
//$logger->log('test', 'something wrong with the inner info', 'MainFlow');
$batchLogger = new Aliyun_Log_Models_LogBatch(25, $logger);

for($i = 1; $i <= 133; $i++){
$batchLogger->log('something wrong with the inner info '.$i,'info');
}

getLogs($client,$project,$logstore);

getLogs($client,$project,$logstore);
8 changes: 4 additions & 4 deletions sample/sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ function logVarDump($expression){
* http://help.aliyun-inc.com/internaldoc/detail/29074.html?spm=0.0.0.0.tqUNn5
*/
$endpoint = 'http://cn-shanghai-corp.sls.aliyuncs.com';
$accessKeyId = 'LTAIUbY1Pk7Ryf1P';
$accessKey = '0oXZLJrFoRnlzVpDpopNVstd87bUWn';
$project = 'ali-sls-sdk-test';
$logstore = 'sls-test';
$accessKeyId = '';
$accessKey = '';
$project = '';
$logstore = '';
$token = "";

$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey,$token);
Expand Down