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

Fr20180213 #11

Merged
merged 2 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
158 changes: 156 additions & 2 deletions Aliyun/Log/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function putLogs(Aliyun_Log_Models_PutLogsRequest $request) {
$logGroup->addLogs ( $log );
}

$body = Aliyun_Log_Util::toBytes ( $logGroup );
$body = Aliyun_Log_Util::toBytes( $logGroup );
unset ( $logGroup );

$bodySize = strlen ( $body );
Expand All @@ -280,6 +280,159 @@ public function putLogs(Aliyun_Log_Models_PutLogsRequest $request) {
return new Aliyun_Log_Models_PutLogsResponse ( $header );
}

/**
* create shipper service
* @param Aliyun_Log_Models_CreateShipperRequest $request
* return Aliyun_Log_Models_CreateShipperResponse
*/
public function createShipper(Aliyun_Log_Models_CreateShipperRequest $request){
$headers = array();
$params = array();
$resource = "/logstores/".$request->getLogStore()."/shipper";
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["Content-Type"] = "application/json";

$body = array(
"shipperName" => $request->getShipperName(),
"targetType" => $request->getTargetType(),
"targetConfiguration" => $request->getTargetConfigration()
);
$body_str = json_encode($body);
$headers["x-log-bodyrawsize"] = strlen($body_str);
list($resp, $header) = $this->send("POST", $project,$body_str,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_CreateShipperResponse($resp, $header);
}

/**
* create shipper service
* @param Aliyun_Log_Models_UpdateShipperRequest $request
* return Aliyun_Log_Models_UpdateShipperResponse
*/
public function updateShipper(Aliyun_Log_Models_UpdateShipperRequest $request){
$headers = array();
$params = array();
$resource = "/logstores/".$request->getLogStore()."/shipper/".$request->getShipperName();
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["Content-Type"] = "application/json";

$body = array(
"shipperName" => $request->getShipperName(),
"targetType" => $request->getTargetType(),
"targetConfiguration" => $request->getTargetConfigration()
);
$body_str = json_encode($body);
$headers["x-log-bodyrawsize"] = strlen($body_str);
list($resp, $header) = $this->send("PUT", $project,$body_str,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_UpdateShipperResponse($resp, $header);
}

/**
* get shipper tasks list, max 48 hours duration supported
* @param Aliyun_Log_Models_GetShipperTasksRequest $request
* return Aliyun_Log_Models_GetShipperTasksResponse
*/
public function getShipperTasks(Aliyun_Log_Models_GetShipperTasksRequest $request){
$headers = array();
$params = array(
'from' => $request->getStartTime(),
'to' => $request->getEndTime(),
'status' => $request->getStatusType(),
'offset' => $request->getOffset(),
'size' => $request->getSize()
);
$resource = "/logstores/".$request->getLogStore()."/shipper/".$request->getShipperName()."/tasks";
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["x-log-bodyrawsize"] = 0;
$headers["Content-Type"] = "application/json";

list($resp, $header) = $this->send("GET", $project,null,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_GetShipperTasksResponse($resp, $header);
}

/**
* retry shipper tasks list by task ids
* @param Aliyun_Log_Models_RetryShipperTasksRequest $request
* return Aliyun_Log_Models_RetryShipperTasksResponse
*/
public function retryShipperTasks(Aliyun_Log_Models_RetryShipperTasksRequest $request){
$headers = array();
$params = array();
$resource = "/logstores/".$request->getLogStore()."/shipper/".$request->getShipperName()."/tasks";
$project = $request->getProject () !== null ? $request->getProject () : '';

$headers["Content-Type"] = "application/json";
$body = $request->getTaskLists();
$body_str = json_encode($body);
$headers["x-log-bodyrawsize"] = strlen($body_str);
list($resp, $header) = $this->send("PUT", $project,$body_str,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_RetryShipperTasksResponse($resp, $header);
}

/**
* delete shipper service
* @param Aliyun_Log_Models_DeleteShipperRequest $request
* return Aliyun_Log_Models_DeleteShipperResponse
*/
public function deleteShipper(Aliyun_Log_Models_DeleteShipperRequest $request){
$headers = array();
$params = array();
$resource = "/logstores/".$request->getLogStore()."/shipper/".$request->getShipperName();
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["x-log-bodyrawsize"] = 0;
$headers["Content-Type"] = "application/json";

list($resp, $header) = $this->send("DELETE", $project,null,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_DeleteShipperResponse($resp, $header);
}

/**
* get shipper config service
* @param Aliyun_Log_Models_GetShipperConfigRequest $request
* return Aliyun_Log_Models_GetShipperConfigResponse
*/
public function getShipperConfig(Aliyun_Log_Models_GetShipperConfigRequest $request){
$headers = array();
$params = array();
$resource = "/logstores/".$request->getLogStore()."/shipper/".$request->getShipperName();
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["x-log-bodyrawsize"] = 0;
$headers["Content-Type"] = "application/json";

list($resp, $header) = $this->send("GET", $project,null,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_GetShipperConfigResponse($resp, $header);
}

/**
* list shipper service
* @param Aliyun_Log_Models_ListShipperRequest $request
* return Aliyun_Log_Models_ListShipperResponse
*/
public function listShipper(Aliyun_Log_Models_ListShipperRequest $request){
$headers = array();
$params = array();
$resource = "/logstores/".$request->getLogStore()."/shipper";
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["x-log-bodyrawsize"] = 0;
$headers["Content-Type"] = "application/json";

list($resp, $header) = $this->send("GET", $project,null,$resource,$params,$headers);
$requestId = isset($header['x-log-requestid']) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson($resp, $requestId);
return new Aliyun_Log_Models_ListShipperResponse($resp, $header);
}

/**
* create logstore
* Unsuccessful opertaion will cause an Aliyun_Log_Exception.
Expand All @@ -301,6 +454,7 @@ public function createLogstore(Aliyun_Log_Models_CreateLogstoreRequest $request)
"shardCount" => (int)($request -> getShardCount())
);
$body_str = json_encode($body);
$headers["x-log-bodyrawsize"] = strlen($body_str);
list($resp,$header) = $this -> send("POST",$project,$body_str,$resource,$params,$headers);
$requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson ( $resp, $requestId );
Expand All @@ -318,7 +472,6 @@ public function updateLogstore(Aliyun_Log_Models_UpdateLogstoreRequest $request)
$headers = array ();
$params = array ();
$project = $request->getProject () !== null ? $request->getProject () : '';
$headers["x-log-bodyrawsize"] = 0;
$headers["Content-Type"] = "application/json";
$body = array(
"logstoreName" => $request -> getLogstore(),
Expand All @@ -327,6 +480,7 @@ public function updateLogstore(Aliyun_Log_Models_UpdateLogstoreRequest $request)
);
$resource = '/logstores/'.$request -> getLogstore();
$body_str = json_encode($body);
$headers["x-log-bodyrawsize"] = strlen($body_str);
list($resp,$header) = $this -> send("PUT",$project,$body_str,$resource,$params,$headers);
$requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
$resp = $this->parseToJson ( $resp, $requestId );
Expand Down
69 changes: 69 additions & 0 deletions Aliyun/Log/LoggerFactory.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_LoggerFactory
* Factory for creating logger instance, with $client, $project, $logstore, $topic configurable.
* Will flush current logger when the factory instance was recycled.
*/
class Aliyun_Log_LoggerFactory{

private static $loggerMap = array();

/**
* Get logger instance
* @param $client valid log client
* @param $project which could be created in AliYun Logger Server configuration page
* @param $logstore which could be created in AliYun Logger Server configuration page
* @param null $topic used to specified the log by TOPIC field
* @return mixed return logger instance
* @throws Exception if the input parameter is invalid, throw exception
*/
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 = '';
}
$loggerKey = $project.'#'.$logstore.'#'.$topic;
if (!array_key_exists($loggerKey, static::$loggerMap))
{
$instanceSimpleLogger = new Aliyun_Log_SimpleLogger($client,$project,$logstore,$topic);
static::$loggerMap[$loggerKey] = $instanceSimpleLogger;
}
return static::$loggerMap[$loggerKey];
}

/**
* set modifier to protected for singleton pattern
* Aliyun_Log_LoggerFactory constructor.
*/
protected function __construct()
{

}

/**
* set clone function to private for singleton pattern
*/
private function __clone()
{}

/**
* flush current logger in destruct function
*/
function __destruct() {
if(static::$loggerMap != null){
foreach (static::$loggerMap as $innerLogger){
$innerLogger->logFlush();
}
}
}
}
92 changes: 92 additions & 0 deletions Aliyun/Log/Models/LogLevel/LogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/

class Aliyun_Log_Models_LogLevel_LogLevel{
const debug = 'debug';
const info = 'info';
const warn = 'warn';
const error = 'error';

private static $constCacheArray = NULL;

private $level;

/**
* Constructor
*
* @param string $level
*/
private function __construct($level) {
$this->level = $level;
}

/**
* Compares two logger levels.
*
* @param LoggerLevels $other
* @return boolean
*/
public function equals($other) {
if($other instanceof Aliyun_Log_Models_LogLevel_LogLevel) {
if($this->level == $other->level) {
return true;
}
} else {
return false;
}
}

public static function getLevelDebug(){
if(!isset(self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::debug])){
self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::debug] = new Aliyun_Log_Models_LogLevel_LogLevel('debug');
}
return self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::debug];
}

public static function getLevelInfo(){
if(!isset(self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::info])){
self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::info] = new Aliyun_Log_Models_LogLevel_LogLevel('info');
}
return self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::info];
}

public static function getLevelWarn(){
if(!isset(self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::warn])){
self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::warn] = new Aliyun_Log_Models_LogLevel_LogLevel('warn');
}
return self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::warn];
}

public static function getLevelError(){
if(!isset(self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::error])){
self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::error] = new Aliyun_Log_Models_LogLevel_LogLevel('error');
}
return self::$constCacheArray[Aliyun_Log_Models_LogLevel_LogLevel::error];
}

public static function getLevelStr(Aliyun_Log_Models_LogLevel_LogLevel $logLevel){

$logLevelStr = '';
if(null === $logLevel){
$logLevelStr = 'info';
}
switch ($logLevel->level){
case "error":
$logLevelStr= 'error';
break;
case "warn":
$logLevelStr= 'warn';
break;
case "info":
$logLevelStr= 'info';
break;
case "debug":
$logLevelStr= 'debug';
break;
}
return $logLevelStr;
}
}
Loading