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
80 changes: 52 additions & 28 deletions Aliyun/Log/Models/LogBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* 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 = [];
Expand All @@ -16,29 +21,49 @@ class Aliyun_Log_Models_LogBatch{

private $shm_id;

public function __construct($maxLogSize, Aliyun_Log_Logger $logger, $sem_id = null, $shm_id = null)
private $topic;

private $waitTime;

private $previousLogTime;

public function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount = null, $cacheLogWaitTime = null)
{
$this->arraySize = $maxLogSize;
if(!is_integer($maxLogSize)){
$this->arraySize = 5;
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;
if($sem_id == null || $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);
$this->topic = $topic;

$MEMSIZE = 10240;
$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对象的问题(这个我不是和确定),如果没有多线程问题,可以不加锁

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

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

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

if(NULL === $prevoidCallTime){
$prevoidCallTime = 0;
}
$this->previousLogTime = time();
$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,
Expand All @@ -52,8 +77,8 @@ public function log($logMessage, $logLevel){
$logItems = shm_get_var($this->shm_id, 1);
array_push($logItems, $logItem);

if(sizeof($logItems) == $this->arraySize){
$this->logger->logBatch($logItems, 'MainFlow');
if((sizeof($logItems) == $this->arraySize) || ($this->previousLogTime - $prevoidCallTime > 5)){
$this->logger->logBatch($logItems, $this->topic);
$logItems = [];
}

Expand All @@ -63,22 +88,21 @@ public function log($logMessage, $logLevel){
}
}

public function shareSem($shm_id){
private function batchSentLog($logItems){

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>";
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, 'MainFlow');
$this->logger->logBatch($this->logItems, $this->topic);
}

sem_remove($this->sem_id);
Expand Down
4 changes: 2 additions & 2 deletions sample/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ 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');
$batchLogger = new Aliyun_Log_Models_LogBatch(25, $logger);
$batchLogger = new Aliyun_Log_Models_LogBatch( $logger,'MainFlow');

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

Expand Down