🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
##概要 > 首先有这么几点需求 > * 在使用的时候可以直接在阿里云控制台上查看日志 > * 正常的访问日志和异常日志要分开存储(方便查看) > * 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文件夹里 ![](https://box.kancloud.cn/2016-07-14_5787861fceeaf.png) 创建一个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] 正常日志 ![](https://box.kancloud.cn/2016-07-14_5787861fe8feb.png) >[danger] 错误日志 ![](https://box.kancloud.cn/2016-07-14_5787862010ca7.png)