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

首頁 > php框架 > YII > 正文

yii怎么記錄api接口執(zhí)行時間

王林
發(fā)布: 2020-02-17 17:17:38
原創(chuàng)
3043人瀏覽過

yii怎么記錄api接口執(zhí)行時間

在?basecontroller?中繼承父類的?beforeaction?和?afteraction?勾子,記錄api運行開始,以及結(jié)束時間。

示例:

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;}
登錄后復(fù)制

(推薦教程:yii框架

logInfo?日志記錄方法,這個方法是對?YII?info日志的二次封裝

// yii日志組件記錄日志if (!function_exists("logInfo")) {
    function logInfo($message, $category = "debug")
    {
        // 記錄info日志,用于調(diào)試
        $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);
    }}
登錄后復(fù)制

日志輸出如下:

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
登錄后復(fù)制

Yii-log 配置如下:

[
    'class' => 'yii\log\FileTarget',
    'levels' => ['info'],
    'categories' => ['debug', 'sql', 'elastic', 'py'],
    'logVars' => [],
    'logFile' => '@runtime/logs/info.log'
]
登錄后復(fù)制

更多編程相關(guān)內(nèi)容,請關(guān)注php中文網(wǎng)編程教程欄目!

以上就是yii怎么記錄api接口執(zhí)行時間的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!

最佳 Windows 性能的頂級免費優(yōu)化軟件
最佳 Windows 性能的頂級免費優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號