阿里云 日志服务接入

目前项目中都有用到日志记录,一般会存到本地,但是时间长了,去删除也是很麻烦的,阿里云日志服务 是个不错的选择

可分为两种,自动采集和自动上传到云

1.写文件到本地,然后配置,让阿里云自动采集

LogHub 支持客户端、网页、协议、SDK/API等多种日志无损采集方式,所有采集方式均基于Restful API实现,除此之外您也可以通过API/SDK实现新的采集方式。

线上服务器安装log采集

服务器命令
wget http://logtail-release-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/linux64/logtail.sh -O logtail.sh;chmod 755 logtail.sh
./logtail.sh install auto      (在root权限下)

# vim /etc/ilogtail/user_defined_id   设置标识  XXX-test

2.通过API或者SDK上传到阿里云

listLogstores($req1);
var_dump($res1);
#创建 logstore
$req2 = new Aliyun_Log_Models_CreateLogstoreRequest($project,$logstore,3,2);
$res2 = $client -> createLogstore($req2);
#等待 logstore 生效
sleep(60);
#写入日志
$topic = "";
$source = "";
$logitems = array();
for ($i = 0; $i < 5; $i++)
{
    $contents = array('index1'=> strval($i));
    $logItem = new Aliyun_Log_Models_LogItem();
    $logItem->setTime(time());
    $logItem->setContents($contents);
    array_push($logitems, $logItem);
}
$req2 = new Aliyun_Log_Models_PutLogsRequest($project, $logstore, $topic, $source, $logitems);
$res2 = $client->putLogs($req2);

 

你可能感兴趣的:(php)