最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

yii怎么记录api接口执行时间

来源:动视网 责编:小采 时间:2020-11-03 18:18:07
文档

yii怎么记录api接口执行时间

yii怎么记录api接口执行时间:在 BaseController 中继承父类的 beforeAction 和 afterAction 勾子,记录API运行开始,以及结束时间。示例:private $actionStart = 0; private $actionEnd = 0; // beforeAction, afterAction 用来记录API请求接口,以
推荐度:
导读yii怎么记录api接口执行时间:在 BaseController 中继承父类的 beforeAction 和 afterAction 勾子,记录API运行开始,以及结束时间。示例:private $actionStart = 0; private $actionEnd = 0; // beforeAction, afterAction 用来记录API请求接口,以


在 BaseController 中继承父类的 beforeAction 和 afterAction 勾子,记录API运行开始,以及结束时间。

示例:

private $actionStart = 0;
private $actionEnd = 0;
// beforeAction, afterAction 用来记录API请求接口,以及耗时
public function beforeAction($action){
 $this->actionStart = microtime(true);
 return parent::beforeAction($action);
 }
 public function afterAction($action, $result){
 $this->actionEnd = microtime(true);
 $afterAction = parent::afterAction($action, $result);
 // 记录API请求接口,耗时took
 logInfo(print_r(["api" => request()->url, "took" => sprintf("%.5f", $this->actionEnd - $this->actionStart)], true));
 return $afterAction;}

(推荐教程:yii框架)

logInfo 日志记录方法,这个方法是对 YII info日志的二次封装

// yii日志组件记录日志if (!function_exists("logInfo")) {
 function logInfo($message, $category = "debug")
 {
 // 记录info日志,用于调试
 $logEnable = Yii::$app->params["log_enable"];
 if (is_null($logEnable) || $logEnable === false) {
 return;
 }
 Yii::info(sprintf("%s
	memory used %d bytes [%.3f KB]", $message, memory_get_usage(), memory_get_usage()/1024), $category);
 }}

日志输出如下:

2019-03-14 02:46:31 [127.0.0.1][-][-][info][debug] Array
(
 [api] => /protocol?page=1&limit=12&unit=10m&time[]=1551854884755&time[]=1552459684755&q=&es_type=http&src_ip=&src_port=&dst_ip=&dst_port=&sensor_id=&uids=&prs_debug=1
 [took] => 0.18194
)

 memory used 8996368 bytes [8785.516 KB]
 in /Users/tophant.yunfei/work/prs-rebirth-php/common/utils/function.php:316
 in /Users/tophant.yunfei/work/prs-rebirth-php/backend/controllers/RestBaseController.php:61

Yii-log 配置如下:

[
 'class' => 'yiilogFileTarget',
 'levels' => ['info'],
 'categories' => ['debug', 'sql', 'elastic', 'py'],
 'logVars' => [],
 'logFile' => '@runtime/logs/info.log'
]

更多编程相关内容,请关注Gxlcms编程教程栏目!

文档

yii怎么记录api接口执行时间

yii怎么记录api接口执行时间:在 BaseController 中继承父类的 beforeAction 和 afterAction 勾子,记录API运行开始,以及结束时间。示例:private $actionStart = 0; private $actionEnd = 0; // beforeAction, afterAction 用来记录API请求接口,以
推荐度:
标签: 时间 API接口 API
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top