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
101 changes: 0 additions & 101 deletions Aliyun/Log/Logger.php

This file was deleted.

31 changes: 17 additions & 14 deletions Aliyun/Log/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@
* All rights reserved
*/

class Aliyun_Log_LoggerFactory extends Aliyun_Log_SimpleLogger{
protected static $instanceLogger = null;
protected static $instanceSimpleLogger = null;
/**
* Class Aliyun_Log_LoggerFactory
*/
class Aliyun_Log_LoggerFactory{

public static function getLogger($client, $project, $logstore){
if (!isset(static::$instanceLogger)) {
static::$instanceLogger = new Aliyun_Log_Logger($client, $project, $logstore);
}
return static::$instanceLogger;
}
private static $loggerMap = array();

public static function getSimpleLogger($client, $project, $logstore, $topic=null){
public static function getLogger($client, $project, $logstore, $topic = null){
if($project === null || $project == ''){
throw new Exception('project name is blank!');
}
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 = ""

}
if (!isset(static::$instanceSimpleLogger)) {
$logger = new Aliyun_Log_Logger($client, $project, $logstore);
static::$instanceSimpleLogger = new Aliyun_Log_SimpleLogger($logger,$topic);
$loggerKey = $project.'#'.$logstore.'#'.$topic;
if (!array_key_exists($loggerKey, static::$loggerMap) || static::$loggerMap[$loggerKey] === null) {
$instanceSimpleLogger = new Aliyun_Log_SimpleLogger($client,$project,$logstore,$topic);
static::$loggerMap[$loggerKey] = $instanceSimpleLogger;
}
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 中,是否还要没有发送出去的数据,如果有的话,需要发送出去

Expand Down
119 changes: 104 additions & 15 deletions Aliyun/Log/SimpleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,37 @@
*/

/**
* 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_SimpleLogger
* simple logger for submit log message
*/
class Aliyun_Log_SimpleLogger extends Aliyun_Log_Logger {
class Aliyun_Log_SimpleLogger{

private $logItems = [];

private $arraySize;

private $logger;

private $topic;

private $waitTime;

private $previousLogTime;

private $client;

private $project;

private $logstore;

/**
* Aliyun_Log_Models_LogBatch constructor.
* @param Aliyun_Log_Logger $logger
* @param $client log client
* @param $project the corresponding project
* @param $logstore the logstore
* @param $topic
* @param null $cacheLogCount max log items limitation, by default it's 100
* @param null $cacheLogWaitTime max thread waiting time, bydefault it's 5 seconds
*/
protected function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount = null, $cacheLogWaitTime = null)
public function __construct($client, $project, $logstore, $topic, $cacheLogCount = null, $cacheLogWaitTime = null)
{
if(NULL === $cacheLogCount || !is_integer($cacheLogCount)){
$this->arraySize = 10;
Expand All @@ -43,8 +48,12 @@ protected function __construct(Aliyun_Log_Logger $logger, $topic, $cacheLogCount
}else{
$this->waitTime = $cacheLogWaitTime;
}

$this->logger = $logger;
if($client == null || $project == null || $logstore == null){
throw new Exception('the input parameter is invalid! create SimpleLogger failed!');
}
$this->client = $client;
$this->project = $project;
$this->logstore = $logstore;
$this->topic = $topic;
}

Expand All @@ -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 就更新

if(null === $previousCallTime){
$previousCallTime = 0;
Expand All @@ -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中。

}else{
$logItems = $this->logItems;
$contents = array( // key-value pair
Expand All @@ -90,26 +99,106 @@ public function log(Aliyun_Log_Models_LogLevel_LogLevel $logLevel,$logMessage, $
if((sizeof($logItems) == $this->arraySize
|| $this->previousLogTime - $previousCallTime > 5000)
&& $previousCallTime > 0){
$this->logger->logBatch($logItems, $this->topic);
$this->logBatch($logItems, $this->topic);
$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中

if(!$logLevel instanceof Aliyun_Log_Models_LogLevel_LogLevel){
throw new Exception('LogLevel value is invalid!');
}
if(is_array($logMessage)){
throw new Exception('array is not supported in this function, please use logArrayMessage!');
}
$ip = $this->getLocalIp();
$contents = array( // key-value pair
'time'=>date('m/d/Y h:i:s a', time()),
'message'=> $logMessage,
'loglevel'=> Aliyun_Log_Models_LogLevel_LogLevel::getLevelStr($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,
$this->topic, $ip, $logitems);
$response = $this->client->putLogs($request);
} catch (Aliyun_Log_Exception $ex) {
var_dump($ex);
} catch (Exception $ex) {
var_dump($ex);
}
}

public function logArrayMessage(Aliyun_Log_Models_LogLevel_LogLevel $logLevel, $logMessage){
if(!$logLevel instanceof Aliyun_Log_Models_LogLevel_LogLevel){
throw new Exception('LogLevel value is invalid!');
}
if(!is_array($logMessage)){
throw new Exception('input message is not array, please use logSingleMessage!');
}
$contents = array( // key-value pair
'time'=>date('m/d/Y h:i:s a', time())
);
$ip = $this->getLocalIp();
if(is_array($logMessage)){
foreach ($logMessage as $key => $value)
$contents[$key] = $value;
}
$contents['logLevel'] = Aliyun_Log_Models_LogLevel_LogLevel::getLevelStr($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,
$this->topic, $ip, $logitems);
$response = $this->client->putLogs($request);
} 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;
}

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

/**
* manually flush all cached log to log server
*/
public function logFlush(){
if(sizeof($this->logItems) > 0){
$this->logger->logBatch($this->logItems, $this->topic);
$this->logBatch($this->logItems, $this->topic);
$this->logItems = [];
}
}

function __destruct() {
if(sizeof($this->logItems) > 0){
$this->logger->logBatch($this->logItems, $this->topic);
$this->logBatch($this->logItems, $this->topic);
}
}
}
14 changes: 7 additions & 7 deletions sample/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ function getLogs(Aliyun_Log_Client $client, $project, $logstore) {
}

// please update the configuration according your profile
$endpoint = 'http://cn-shanghai-corp.sls.aliyuncs.com';
$accessKeyId = 'LTAIUbY1Pk7Ryf1P';
$accessKey = '0oXZLJrFoRnlzVpDpopNVstd87bUWn';
$project = 'ali-sls-sdk-test';
$endpoint = '';
$accessKeyId = '';
$accessKey = '';
$project = '';
$logstore = 'test';
$token = "";

Expand All @@ -89,12 +89,12 @@ function getLogs(Aliyun_Log_Client $client, $project, $logstore) {
$logger->log(Aliyun_Log_Models_LogLevel_LogLevel::getLevelInfo(),'test INFO LOG', 'MainFlow');

$logger2 = Aliyun_Log_LoggerFactory::getLogger($client, $project, $logstore);
$logger2->log(Aliyun_Log_Models_LogLevel_LogLevel::getLevelInfo(),'test INFO LOG2222222', 'MainFlow');
$logger2->logSingleMessage(Aliyun_Log_Models_LogLevel_LogLevel::getLevelInfo(),'test INFO LOG2222222', 'MainFlow');

$logger->logArray(Aliyun_Log_Models_LogLevel_LogLevel::getLevelInfo(),$logMap, 'MainFlow');
$logger->logArrayMessage(Aliyun_Log_Models_LogLevel_LogLevel::getLevelInfo(),$logMap, 'MainFlow');

//$logger->log('test', 'something wrong with the inner info', 'MainFlow');
$batchLogger = Aliyun_Log_LoggerFactory::getSimpleLogger($client, $project, $logstore);
$batchLogger = Aliyun_Log_LoggerFactory::getLogger($client, $project, $logstore,'helloworld');

for($i = 1; $i <= 29; $i++){
$batchLogger->log(Aliyun_Log_Models_LogLevel_LogLevel::getLevelInfo(),'something wrong with the inner info '.$i);
Expand Down