真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Yii2框架設(shè)置錯誤日志輸出到日志或數(shù)據(jù)庫

設(shè)置Yii的錯誤日志,可按照設(shè)置錯誤級別輸入到日志或數(shù)據(jù)庫中。
對yii\log\FileTarget做了重寫。

東坡ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

1、設(shè)置common/config/main.php
$db = require(__DIR__ . '/db.php');
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'bootstrap' => ['log'],
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'db' => $db,
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning', 'info'],
                ],
                [
//                    'class' => 'yii\log\FileTarget',
                    'class' => 'app\common\components\FileTarget',
                    'levels' => ['error'],
                    'logFile' => '@runtime/logs/error.log',
                ],//日志入庫
            ],
        ],
    ],
    'timeZone' => 'Asia/Shanghai',
];

2、app\common\components\FileTarget

params['errorLog']['switch'])) {//設(shè)置參數(shù)是否收集錯誤日志

            $flag = false;
            if (empty($_SERVER['DOCUMENT_ROOT'])) {
                $projectName = '';
                $entrance = 'console';
                $flag = true;
            } else {
                $document_root = explode('/', $_SERVER['DOCUMENT_ROOT']);
                $projectName = $document_root[count($document_root) - 3];//項目名稱
                $entrance = $document_root[count($document_root) - 2];//項目入口
            }

            $params = $_POST ?: $_GET;
            $errList = [];
            foreach ($this->messages as $message) {
                list($text, $level, $category, $timestamp) = $message;
                if ($level <> 4) {
                    if (!is_string($text)) {
                        // exceptions may not be serializable if in the call stack somewhere is a Closure
                        if ($text instanceof \Throwable || $text instanceof \Exception) {
                            $text = (string)$text;
                        } else {
                            $text = VarDumper::export($text);
                        }
                    }

                    if ($flag) {
                        $dir1 = '/var/www/html/';
                        $start = strpos($text, $dir1);
                        if (!$start) {
                            $dir1 = '/disk/html/';
                            $start = strpos($text, $dir1);
                            $project = substr($text, $start + strlen($dir1), 50);
                            $project = explode('/', $project);
                            $projectName = $project[0];

                        } else {
                            $project = substr($text, $start + strlen($dir1), 50);
                            $project = explode('/', $project);
                            $projectName = $project[0];
                        }
                    }

                    $text = $text . "\n" . "params:" . json_encode($params);
                    $errType = 'error';
                    if (stripos($category, 'db')) {
                        $errType = 'db';
                    } else if (stripos($text, 'redis')) {
                        $errType = 'redis';
                    }
                    $item = [];
                    $item[] = empty($projectName) ? '0' : $projectName;//項目
                    $item[] = empty($entrance) ? '0' : $entrance;//入口
                    $item[] = empty($_SERVER['SERVER_NAME']) ? '0' : $_SERVER['SERVER_NAME'];
                    $item[] = $level;
                    $item[] = $category;
                    $item[] = $timestamp;
                    $item[] = $this->getMessagePrefix($message);
                    $item[] = IPUtils::get_local_ip();
                    $item[] = empty($_SERVER['REQUEST_URI']) ? '0' : $_SERVER['REQUEST_URI'];
                    $item[] = $errType;
                    $item[] = $text;//錯誤信息
                    $errList[] = $item;
                }
            }
            //請求接口入庫
            $this->collectErrorLog($errList);
        }
    }

    /**
     * @param $data
     * @return mixed
     * 錯誤日志入庫
     */
    private function collectErrorLog($data)
    {
        if (empty(\Yii::$app->params['errorLog']['logApiDomain'])) {
            if (YII_ENV == 'local' || YII_ENV == 'dev') {
                $url = 'http://dev.api.log.3ttech.cn/inner/collect-error-log?';
            } else {
                $url = 'http://api.log.3ttech.cn/inner/collect-error-log?';
            }
        } else {
            $url = \Yii::$app->params['errorLog']['logApiDomain'] . '/inner/collect-error-log?';
        }
        $requestBody = [
            'data' => $data,
        ];
        $url .= http_build_query($requestBody);
        $response = json_decode(AHelper::curl_get($url), true);
        return $response;
    }
}

3、數(shù)據(jù)表的結(jié)構(gòu)如下:
表字段可按照yii2框架中的字段調(diào)整設(shè)置。

CREATE TABLE `t_user_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `project` varchar(100) NOT NULL DEFAULT '' COMMENT '項目名稱',
  `entrance` varchar(20) NOT NULL DEFAULT '' COMMENT '入口',
  `address` varchar(50) NOT NULL DEFAULT '' COMMENT '訪問域名',
  `level` varchar(200) NOT NULL DEFAULT '' COMMENT '錯誤級別',
  `category` varchar(200) NOT NULL DEFAULT '' COMMENT '類別',
  `log_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '日志時間',
  `prefix` varchar(200) NOT NULL DEFAULT '' COMMENT '前綴',
  `suffix` varchar(200) NOT NULL DEFAULT '' COMMENT '報錯

當(dāng)前名稱:Yii2框架設(shè)置錯誤日志輸出到日志或數(shù)據(jù)庫
分享地址:
http://weahome.cn/article/pcscjc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部