亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解Yii如何使用DbTarget實現日志功能

發布時間:2020-07-21 13:51:21 來源:億速云 閱讀:224 作者:小豬 欄目:開發技術

小編這次要給大家分享的是詳解Yii如何使用DbTarget實現日志功能,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

一:在配置文件的log組件中配置DbTarget

詳解Yii如何使用DbTarget實現日志功能

'log' => [
 'traceLevel' => YII_DEBUG ? 3 : 0,
 'targets' => [
  [
   'class' => 'yii\log\FileTarget',
   'levels' => ['error', 'warning'],
  ],
  'test' => [
   'class' => 'yii\log\DbTarget',//DaTarget類
   'logTable' => '{{%test_log}}',//日志表
   'levels' => ['error', 'info', 'warning'],//日志等級
  ],
 ],
],

二:生成日志表

在項目目錄下執行如下命令生成日志表

php yii migrate --migrationPath=@yii/log/migrations/

三:使用日志

在需要使用日志的地方使用

Yii::info()

四:自定義DbTarget日志

1:首先創建一個自定義的日志表

(1)在項目目錄下執行

php yii migrate/create create_test_log

(2):在創建的migrate文件下編寫創建數據庫的遷移腳本

<&#63;php
use yii\db\Migration;
/**
 * Class m200720_091126_create_test_log
 */
class m200720_091126_create_test_log extends Migration
{
 /**
  * {@inheritdoc}
  */
 public function safeUp()
 {
  $this->createTable('{{%test_log}}', [
   'id' => $this->bigPrimaryKey(),
   'level' => $this->integer()->notNull()->comment('日志等級'),
   'category' => $this->string(100)->notNull()->comment('分類名稱'),
   'prefix' => $this->text(),
   'route' => $this->string(100)->notNull()->comment('路由'),
   'method' => $this->string(20)->notNull()->comment('請求方式'),
   'app' => $this->string(20)->comment('請求應用'),
   'module' => $this->string(20)->comment('請求模塊'),
   'request' => $this->text()->comment('請求參數'),
   'status' => $this->string(10)->notNull()->comment('狀態碼'),
   'message' => $this->text()->comment('日志內容'),
   'request_at' => $this->double()->notNull()->comment('請求時間'),
   'ip' => $this->string(63)->comment('請求IP'),
  ], 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB COMMENT=\'請求日志表\'');
  //增加索引
  $this->createIndex('idx_log_level', '{{%test_log}}', 'level');
  $this->createIndex('idx_log_category', '{{%test_log}}', 'category');
  $this->createIndex('idx_log_route', '{{%test_log}}', 'route');
  $this->createIndex('idx_log_method', '{{%test_log}}', 'method');
  $this->createIndex('idx_log_status', '{{%test_log}}', 'status');
 }
 /**
  * @inheritdoc
  */
 public function safeDown()
 {
  $this->dropTable('{{%test_log}}');
 }
}

(3):執行如下命令生成DbTarget日志表

php yii migrate

2:編寫一個DbTarget類來繼承yiilogDbTarget類

<&#63;php
namespace app\components;
use Yii;
use yii\helpers\VarDumper;
use yii\log\LogRuntimeException;
use yii\web\HttpException;
use yii\web\Request;
/**
 * DbTarget stores log messages in a database table.
 *
 * @see yii\log\DbTarget
 *
 * @author wangjian
 * @since 1.0
 */
class DbTarget extends \yii\log\DbTarget
{
 /**
  * @inheritdoc
  */
 public $categories = [
  'application',
  'yii\web\HttpException:*',
 ];
 /**
  * @inheritdoc
  */
 public $except = [
  // 'yii\web\HttpException:404',
 ];
 /**
  * @inheritdoc
  */
 public $logVars = ['_GET', '_POST'];
 /**
  * @var string 用戶組件ID
  */
 public $userComponentId = 'user';
 /**
  * @inheritdoc
  */
 public function collect($messages, $final)
 {
  $this->messages = array_merge($this->messages, static::filterMessages($messages, $this->getLevels(), $this->categories, $this->except));
  $count = count($this->messages);
  if ($count > 0 && ($final || $this->exportInterval > 0 && $count >= $this->exportInterval)) {
   $oldExportInterval = $this->exportInterval;
   $this->exportInterval = 0;
   $this->export();
   $this->exportInterval = $oldExportInterval;
   $this->messages = [];
  }
 }
 /**
  * @inheritdoc
  */
 public function getMessagePrefix($message)
 {
  if ($this->prefix !== null) {
   return call_user_func($this->prefix, $message);
  }
  if (Yii::$app === null) {
   return '';
  }
  $ip = $this->getIp();
  $ip = empty($ip) &#63; '-' : $ip;
  return "[$ip]";
 }
 /**
  * @inheritdoc
  */
 public function export()
 {
  if ($this->db->getTransaction()) {
   $this->db = clone $this->db;
  }
  $tableName = $this->db->quoteTableName($this->logTable);
  $sql = "INSERT INTO $tableName ([[level]], [[category]], [[prefix]], [[route]], [[method]], [[app]], [[module]], [[request]], [[status]], [[message]], [[request_at]], [[ip]])
    VALUES (:level, :category, :prefix, :route, :method, :app, :module, :request, :status, :message, :request_at, :ip)";
  $command = $this->db->createCommand($sql);
  $request = Yii::$app->getRequest();
  list($route, $params) = $request->resolve();
  $method = $request->getMethod();
  $module = Yii::$app->controller->module->id;
  $route = str_replace("{$module}/", '', $route);
  foreach ($this->messages as $message) {
   list($text, $level, $category, $timestamp) = $message;
   $statusCode = 200;
   if (!is_string($text)) {
    if ($text instanceof \Throwable || $text instanceof \Exception) {
     $statusCode = $text instanceof HttpException &#63; $text->statusCode : 500;
     $text = $text->getMessage();
    } else {
     $text = VarDumper::export($text);
    }
   }
   if ($command->bindValues([
     ':level' => $level,
     ':category' => $category,
     ':prefix' => $this->getMessagePrefix($message),
     ':route' => $route,
     ':method' => $method,
     ':app' => Yii::$app->id,
     ':module' => $module,
     ':request' => $this->getContextMessage(),
     ':status' => $statusCode,
     ':message' => $text,
     ':request_at' => $timestamp,
     ':ip' => $this->getIp(),
    ])->execute() > 0) {
    continue;
   }
   throw new LogRuntimeException('Unable to export log through database!');
  }
 }
 /**
  * 獲取當前IP
  */
 protected function getIp()
 {
  $request = Yii::$app->getRequest();
  return $request instanceof Request &#63; $request->getUserIP() : '';
 }
}

3:在配置文件中將yiilogDbTarget類改成我們自定義的類

'log' => [
 'traceLevel' => YII_DEBUG &#63; 3 : 0,
 'targets' => [
  [
   'class' => 'yii\log\FileTarget',
   'levels' => ['error', 'warning'],
  ],
  'test' => [
   'class' => 'app\components\DbTarget',
   'logTable' => '{{%test_log}}',
   'levels' => ['error', 'info', 'warning'],
  ],
 ],
],

4:使用DbTarget日志

同樣的使用Yii::info來記錄日志

看完這篇關于詳解Yii如何使用DbTarget實現日志功能的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

河池市| 茶陵县| 海盐县| 阿鲁科尔沁旗| 昌江| 日喀则市| 昌吉市| 贡山| 犍为县| 东安县| 和龙市| 连云港市| 星座| 胶州市| 方山县| 阳东县| 余江县| 千阳县| 永泰县| 塘沽区| 红安县| 通渭县| 永定县| 名山县| 曲麻莱县| 修水县| 潍坊市| 安宁市| 和田县| 彰化县| 师宗县| 始兴县| 诸城市| 辉县市| 宝清县| 繁峙县| 迭部县| 施秉县| 万载县| 宽甸| 嘉定区|