Home PHP Framework YII How to record API interface execution time in Yii

How to record API interface execution time in Yii

Feb 17, 2020 pm 05:17 PM
api yii

How to record API interface execution time in Yii

Inherit the beforeAction and afterAction hooks of the parent class in BaseController to record the start and end time of API running.

Example:

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;}
Copy after login

(Recommended tutorial: yii framework)

logInfo logging method, this method is a secondary encapsulation of YII info log

// 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\n\tmemory used %d bytes [%.3f KB]", $message, memory_get_usage(), memory_get_usage()/1024), $category);
    }}
Copy after login

The log output is as follows:

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
Copy after login

Yii-log configuration is as follows:

[
    'class' => 'yii\log\FileTarget',
    'levels' => ['info'],
    'categories' => ['debug', 'sql', 'elastic', 'py'],
    'logVars' => [],
    'logFile' => '@runtime/logs/info.log'
]
Copy after login

For more programming related content, please pay attention to the php Chinese websiteProgramming Tutorial Column!

The above is the detailed content of How to record API interface execution time in Yii. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to crawl and process data by calling API interface in PHP project? How to crawl and process data by calling API interface in PHP project? Sep 05, 2023 am 08:41 AM

How to crawl and process data by calling API interface in PHP project?

React API Call Guide: How to interact and transfer data with the backend API React API Call Guide: How to interact and transfer data with the backend API Sep 26, 2023 am 10:19 AM

React API Call Guide: How to interact and transfer data with the backend API

Oracle API Usage Guide: Exploring Data Interface Technology Oracle API Usage Guide: Exploring Data Interface Technology Mar 07, 2024 am 11:12 AM

Oracle API Usage Guide: Exploring Data Interface Technology

Oracle API integration strategy analysis: achieving seamless communication between systems Oracle API integration strategy analysis: achieving seamless communication between systems Mar 07, 2024 pm 10:09 PM

Oracle API integration strategy analysis: achieving seamless communication between systems

Save API data to CSV format using Python Save API data to CSV format using Python Aug 31, 2023 pm 09:09 PM

Save API data to CSV format using Python

How to deal with Laravel API error problems How to deal with Laravel API error problems Mar 06, 2024 pm 05:18 PM

How to deal with Laravel API error problems

PHP API Interface: Getting Started Guide PHP API Interface: Getting Started Guide Aug 25, 2023 am 11:45 AM

PHP API Interface: Getting Started Guide

RESTful API development with Laravel: Building modern web services RESTful API development with Laravel: Building modern web services Aug 13, 2023 pm 01:00 PM

RESTful API development with Laravel: Building modern web services

See all articles