国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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;}

(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);
    }}

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

Yii-log configuration is as follows:

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

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 AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

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

Oracle is a world-renowned database management system provider, and its API (Application Programming Interface) is a powerful tool that helps developers easily interact and integrate with Oracle databases. In this article, we will delve into the Oracle API usage guide, show readers how to utilize data interface technology during the development process, and provide specific code examples. 1.Oracle

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

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

OracleAPI integration strategy analysis: To achieve seamless communication between systems, specific code examples are required. In today's digital era, internal enterprise systems need to communicate with each other and share data, and OracleAPI is one of the important tools to help achieve seamless communication between systems. This article will start with the basic concepts and principles of OracleAPI, explore API integration strategies, and finally give specific code examples to help readers better understand and apply OracleAPI. 1. Basic Oracle API

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

Title: How to deal with Laravel API error problems, specific code examples are needed. When developing Laravel, API errors are often encountered. These errors may come from various reasons such as program code logic errors, database query problems, or external API request failures. How to handle these error reports is a key issue. This article will use specific code examples to demonstrate how to effectively handle Laravel API error reports. 1. Error handling in Laravel

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

ReactAPI Call Guide: How to interact with and transfer data to the backend API Overview: In modern web development, interacting with and transferring data to the backend API is a common need. React, as a popular front-end framework, provides some powerful tools and features to simplify this process. This article will introduce how to use React to call the backend API, including basic GET and POST requests, and provide specific code examples. Install the required dependencies: First, make sure Axi is installed in the project

Insomnia Tutorial: How to use the PHP API interface Insomnia Tutorial: How to use the PHP API interface Jan 22, 2024 am 11:21 AM

PHP API interface: How to use Insomnia Insomnia is a powerful API testing and debugging tool. It can help developers quickly and easily test and verify API interfaces. It supports multiple programming languages ????and protocols, including PHP. This article will introduce how to use Insomnia to test PHPAPI interface. Step 1: Install InsomniaInsomnia is a cross-platform application that supports Windows, MacOS, and Linux.

How to develop a RESTful-based API using Java How to develop a RESTful-based API using Java Sep 21, 2023 pm 03:53 PM

How to use Java to develop a RESTful-based API. RESTful is an architectural style based on the HTTP protocol. It uses the GET, POST, PUT, DELETE and other methods of the HTTP protocol to operate resources. In Java development, some frameworks can be used to simplify the development process of RESTful API, such as SpringMVC, Jersey, etc. This article will introduce you in detail how to use Java to develop a RESTful-based

How to develop a simple CRUD API using MongoDB How to develop a simple CRUD API using MongoDB Sep 19, 2023 pm 12:32 PM

How to use MongoDB to develop a simple CRUD API In modern web application development, CRUD (Create, Delete, Modify, Check) operations are one of the most common and important functions. In this article, we will introduce how to develop a simple CRUD API using MongoDB database and provide specific code examples. MongoDB is an open source NoSQL database that stores data in the form of documents. Unlike traditional relational databases, MongoDB does not have a predefined schema

See all articles