Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
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
69 changes: 69 additions & 0 deletions Aliyun/Log/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/

class Aliyun_Log_Logger{

protected $client;

protected $project;

protected $logstore;

public function __construct($client, $project, $logstore)
{
$this ->client = $client;
$this->logstore=$logstore;
$this->project=$project;
}

public function log($logLevel, $logMessage, $topic){
if(!Aliyun_Log_Models_logLevel_LogLevel::isValidValue($logLevel)){
throw new Exception('logLevel value is invalid!');
}
$ip = $this->getLocalIp();
$contents = array( // key-value pair
'time'=>date('m/d/Y h:i:s a', time()),
'message'=> $logMessage,
'loglevel'=> $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,
$topic, $ip, $logitems);
$response = $this->client->putLogs($request);
print($response ->getRequestId());
} catch (Aliyun_Log_Exception $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) {
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);
}
}

private function getLocalIp(){
$local_ip = getHostByName(php_uname('n'));
if(strlen($local_ip) == 0){
$local_ip = getHostByName(getHostName());
}
return $local_ip;
}
}
111 changes: 111 additions & 0 deletions Aliyun/Log/Models/LogBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* 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 = [];

private $arraySize;

private $logger;

private $sem_id;

private $shm_id;

private $topic;

private $waitTime;

private $previousLogTime;

public function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount = null, $cacheLogWaitTime = null)
{
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;
$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,
'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->previousLogTime - $prevoidCallTime > 5)){
$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;
}
}

private function batchSentLog($logItems){

}

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, $this->topic);
}

sem_remove($this->sem_id);
shm_remove ($this->shm_id);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/
require_once realpath(dirname(__FILE__) . '/Request.php');
/**
*
*
* @author log service dev
*/
class Aliyun_Log_Models_ApplyConfigToMachineGroupRequest extends Aliyun_Log_Models_Request {
private $groupName;
private $configName;
/**
* Aliyun_Log_Models_ApplyConfigToMachineGroupRequest Constructor
*
*/
public function __construct($groupName=null,$configName=null) {
$this->groupName = $groupName;
$this->configName = $configName;
}
public function getGroupName(){
return $this->groupName;
}
public function setGroupName($groupName){
$this->groupName = $groupName;
}
public function getConfigName(){
return $this->configName;
}
public function setConfigName($configName){
$this->configName = $configName;
}
}
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/

require_once realpath(dirname(__FILE__) . '/Request.php');

/**
*
*
* @author log service dev
*/
class Aliyun_Log_Models_ApplyConfigToMachineGroupRequest extends Aliyun_Log_Models_Request {
private $groupName;
private $configName;
/**
* Aliyun_Log_Models_ApplyConfigToMachineGroupRequest Constructor
*
*/
public function __construct($groupName=null,$configName=null) {
$this->groupName = $groupName;
$this->configName = $configName;
}
public function getGroupName(){
return $this->groupName;
}
public function setGroupName($groupName){
$this->groupName = $groupName;
}

public function getConfigName(){
return $this->configName;
}
public function setConfigName($configName){
$this->configName = $configName;
}

}
Loading