API日志

2018-05-31 18:05 更新

ApiAdmin详细的记录了全部的API请求日志。我们将简要说明,API日志记录可以很好的帮助你调试,定位问题或者说“甩锅”。这里就来说说API日志。

日志源码

<?php
/**
 * 日志记录
 * @since   2017-04-14
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */
namespace Home\ORG;
class ApiLog {
    private static $appInfo = 'null';
    private static $appInfoArr = array();
    private static $apiInfo = 'null';
    private static $request = 'null';
    private static $requestAfterFilter = 'null';
    private static $response = 'null';
    private static $header = 'null';
    private static $userInfo = 'null';
    private static $separator = '###';

    
    /* 记录APP信息 */
    public static function setAppInfo($data) {
        self::$appInfoArr = $data;
        self::$appInfo = $data['app_id'] . self::$separator . $data['app_name'] . self::$separator . $data['device_id'];
    }


    /**
     * @return array
     */
    public static function getAppInfo() {
        return self::$appInfoArr;
    }


    /* 记录Header头信息 */
    public static function setHeader($data) {
        $userToken = (isset($data['USER-TOKEN']) && !empty($data['USER-TOKEN'])) ? $data['USER-TOKEN'] : 'null';
        $accessToken = (isset($data['ACCESS-TOKEN']) && !empty($data['ACCESS-TOKEN'])) ? $data['ACCESS-TOKEN'] : 'null';
        $cas = (isset($data['CAS']) && !empty($data['CAS'])) ? $data['CAS'] : 'null';
        self::$header = $accessToken . self::$separator . $userToken . self::$separator . $data['VERSION'] . self::$separator . $cas;
    }

    
    /* 设置Api信息 */
    public static function setApiInfo($data) {
        self::$apiInfo = $data['apiName'] . self::$separator . $data['hash'];
    }


    /* 设置用户信息 */
    public static function setUserInfo($data) {
        if (is_array($data)) {
            $data = json_encode($data);
        }
        self::$userInfo = $data;
    }


    /* 记录请求信息 */
    public static function setRequest($data) {
        if (is_array($data)) {
            $data = json_encode($data);
        }
        self::$request = $data;
    }


    /* 记录过滤后请求信息 */
    public static function setRequestAfterFilter($data) {
        if (is_array($data)) {
            $data = json_encode($data);
        }
        self::$requestAfterFilter = $data;
    }


    /* 记录返回信息 */
    public static function setResponse($data, $code) {
        if (is_array($data)) {
            $data = json_encode($data);
        }
        self::$response = $code . self::$separator . $data;
    }


    public static function save() {
        $logPath = APP_PATH . '/ApiLog/' . date('YmdH') . '.log';
        if (self::$appInfo == 'null') {
            self::$appInfo = 'null' . self::$separator . 'null' . self::$separator . 'null';
        }
        $logStr = implode(self::$separator, array(
            self::$apiInfo,
            date('Y-m-d H:i:s'),
            self::$request,
            self::$header,
            self::$response,
            self::$requestAfterFilter,
            self::$appInfo,
            self::$userInfo
        ));


        @file_put_contents($logPath, $logStr . "\n", FILE_APPEND);
    }
}

日志原文

BuildToken/getAccessToken###58bf98c1dcb63###2017-05-05 16:44:05###{"m":"Home","city":"nj","timestamp":"1493973845","app_id":"35268647","rand_str":"abcdefghijklmn","signature":"9ebcfbcc70049d68b2428705791484e4","device_id":"null"}###null###null###v1.0###null###1###{"code":1,"msg":"\u64cd\u4f5c\u6210\u529f","data":{"access_token":"a1c66a465e4c380cd7c2a67cfa2d2b84","expires_in":7200},"debug":["9ebcfbcc70049d68b2428705791484e4"]}###{"timestamp":1493973845,"app_id":"35268647","rand_str":"abcdefghijklmn","signature":"9ebcfbcc70049d68b2428705791484e4","device_id":"00000000-7deb-db10-0000-0000227923fd"}###35268647###null###1234556###null

分析后日志

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号