##概要
> 首先有這么幾點需求
> * 在使用的時候可以直接在阿里云控制臺上查看日志
> * 正常的訪問日志和異常日志要分開存儲(方便查看)
> * TP5的正常的文件日志還繼續保留使用
### 1.入門資料
1. [阿里日志服務官方文檔](https://help.aliyun.com/product/8314976_28958.html?spm=5176.750001.3.27.LK8vRp)
2. [TP官方集成sae的日志的github](https://github.com/top-think/think-sae)
## 2.集成SLS的php的SDK
由于需要做統一規劃,并且方便日后更換新的日志系統或者在多個子項目里使用,故最好創建一個自己的composer庫,專門用于集成日志服務
> [點擊下載阿里日志的PHPSDK](https://help.aliyun.com/document_detail/29075.html?spm=5176.doc29074.2.9.SjYa0S)
### 2.1 創建一個第三方庫命名為ifu/log,如何創建可以參考**自定義第三方的composer**
~~~
{
"name": "ifu/helper",
"version": "0.0.1",
"autoload": {
"psr-4": {"ifu\\helper\\": "src"}
},
"description": "ifu helper",
"license": "Apache-2.0",
"authors": [
{
"name": "carlosk",
"email": "carlosk@163.com"
}
],
"require": {
}
}
~~~
### 2.2 把aliyun-sls-sdk包拷貝到src文件夾里

創建一個Log.php文件.具體如下
~~~
<?php
namespace ifu\helper;
require_once("aliyun-sls-sdk/Log_Autoload.php");
/**
* 本地化調試輸出到文件
*/
class Log
{
protected $config = [
'time_format' => 'Y-m-d H:i:s',
'file_size' => 2097152,
'path' => LOG_PATH,
'endpoint' => 'cn-hangzhou.sls.aliyuncs.com',
'accessKeyId' => '',
'accessKey' => '',
'project' => '',
];
protected $client;
// 實例化并傳入參數
public function __construct($config = [])
{
if (is_array($config)) {
$this->config = array_merge($this->config, $config);
}
$endpoint = $this->config['endpoint']; // 選擇與上面步驟創建Project所屬區域匹配的Endpoint
$accessKeyId = $this->config['accessKeyId']; // 使用你的阿里云訪問秘鑰AccessKeyId
$accessKey = $this->config['accessKey']; // 使用你的阿里云訪問秘鑰AccessKeySecret
$this->client = new \Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey);
}
/**
* 保存到阿里云日志里
* @param $source
* @param $contents
*/
private function saveToAli($source, $contents,$logType)
{
$project = $this->config['project']; // 上面步驟創建的項目名稱
$logItem = new \Aliyun_Log_Models_LogItem();
$logItem->setTime(time());
$logItem->setContents($contents);
$logitems = array();
array_push($logitems, $logItem);
$topic = "";
$req2 = new \Aliyun_Log_Models_PutLogsRequest($project, "access", $topic, $source, $logitems);
$this->client->putLogs($req2);
// dump($result);
if($logType == "error"){
//如果是錯誤的話,則也記錄在錯誤日志里
$req2 = new \Aliyun_Log_Models_PutLogsRequest($project, "error", $topic, $source, $logitems);
$this->client->putLogs($req2);
}
// dump($res2);
}
/**
* 日志寫入接口
* @access public
* @param array $log 日志信息
* @return bool
*/
public function save(array $log = [])
{
$now = date($this->config['time_format']);
$destination = $this->config['path'] . date('y_m_d') . '.log';
!is_dir($this->config['path']) && mkdir($this->config['path'], 0755, true);
//檢測日志文件大小,超過配置大小則備份日志文件重新生成
if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) {
rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination));
}
// 獲取基本信息
if (isset($_SERVER['HTTP_HOST'])) {
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
}
$runtime = (number_format(microtime(true), 8, '.', '') - THINK_START_TIME) ?: 0.00000001;
$reqs = number_format(1 / number_format($runtime, 8), 2);
$time_str = ' [運行時間:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
$memory_str = ' [內存消耗:' . $memory_use . 'kb]';
$file_load = ' [文件加載:' . count(get_included_files()) . ']';
$contents = [];
$info = '[ log ] ' . $current_uri . $time_str . $memory_str . $file_load . "\r\n";
$contents['log'] = $current_uri . $time_str . $memory_str . $file_load;
$logType = "info";
foreach ($log as $type => $val) {
if($type == "error"){
$logType = $type;
}
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$info .= '[ ' . $type . ' ] ' . $msg . "\r\n";
}
if (is_array($val)) {
foreach ($val as $key => $value) {
$contents[strtoupper("[ $type $key ]")] = is_object($value) ? json_encode($value) : $value;
}
} else {
$contents[strtoupper("[ $type ]")] = is_object($val) ? json_encode($val) : $val;
}
}
$server = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0';
$remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'CLI';
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$this->saveToAli($server, $contents,$logType);
return error_log("[{$now}] {$server} {$remote} {$method} {$uri}\r\n{$info}\r\n", 3, $destination);
}
}
~~~
### 2.3 項目代碼里的配置如下
~~~
'log'=>[
'type'=> '\ifu\helper\Log',
'accessKeyId' => 'xxxx',
'accessKey' => 'xxxxx',
'project' => 'ifu-test',
],
~~~
### 2.4 在阿里云控制臺上查看相關的日志信息
>[info] 正常日志

>[danger] 錯誤日志
