設置Yii的錯誤日志,可按照設置錯誤級別輸入到日志或數(shù)據(jù)庫中。
對yii\log\FileTarget做了重寫。
東坡ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
1、設置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
<?php
namespace app\common\components;
use yii\helpers\VarDumper;
use yii\log;
class FileTarget extends log\FileTarget
{
public function export()
{
parent::export(); // TODO: Change the autogenerated stub
//調用接口將數(shù)據(jù)入庫 默認是收集錯誤日志
if (empty(\Yii::$app->params['errorLog']['switch'])) {//設置參數(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ù)表的結構如下:
表字段可按照yii2框架中的字段調整設置。
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 '報錯
文章標題:Yii2框架設置錯誤日志輸出到日志或數(shù)據(jù)庫
網(wǎng)站路徑:http://aaarwkj.com/article22/pcscjc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、用戶體驗、標簽優(yōu)化、外貿(mào)網(wǎng)站建設、網(wǎng)站導航、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)