欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii使用DbTarget實現(xiàn)日志功能的示例代碼

 更新時間:2020年07月21日 11:12:36   作者:huaweichenai  
這篇文章主要介紹了Yii使用DbTarget實現(xiàn)日志功能的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧

一:在配置文件的log組件中配置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'],//日志等級
  ],
 ],
],

二:生成日志表

在項目目錄下執(zhí)行如下命令生成日志表

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

三:使用日志

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

Yii::info()

四:自定義DbTarget日志

1:首先創(chuàng)建一個自定義的日志表

(1)在項目目錄下執(zhí)行

php yii migrate/create create_test_log

(2):在創(chuàng)建的migrate文件下編寫創(chuàng)建數(shù)據(jù)庫的遷移腳本

<?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('請求應(yīng)用'),
   'module' => $this->string(20)->comment('請求模塊'),
   'request' => $this->text()->comment('請求參數(shù)'),
   'status' => $this->string(10)->notNull()->comment('狀態(tài)碼'),
   'message' => $this->text()->comment('日志內(nèi)容'),
   '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):執(zhí)行如下命令生成DbTarget日志表

php yii migrate

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

<?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) ? '-' : $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 ? $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 ? $request->getUserIP() : '';
 }
}

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

'log' => [
 'traceLevel' => YII_DEBUG ? 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來記錄日志

到此這篇關(guān)于Yii使用DbTarget實現(xiàn)日志功能的示例代碼的文章就介紹到這了,更多相關(guān)Yii DbTarget 日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • php基于redis的分布式鎖實例詳解

    php基于redis的分布式鎖實例詳解

    這篇文章主要介紹了php基于redis的分布式鎖實例詳解,有正好需要的可以跟著小編一起來學(xué)習下,可以讓在寫代碼上的能力更近一步
    2021-03-03
  • thinkPHP框架實現(xiàn)類似java過濾器的簡單方法示例

    thinkPHP框架實現(xiàn)類似java過濾器的簡單方法示例

    這篇文章主要介紹了thinkPHP框架實現(xiàn)類似java過濾器的簡單方法,結(jié)合實例形式分析了thinkPHP基于繼承實現(xiàn)的登錄驗證功能相關(guān)操作方法,需要的朋友可以參考下
    2018-09-09
  • php解析url并得到url中的參數(shù)及獲取url參數(shù)的四種方式

    php解析url并得到url中的參數(shù)及獲取url參數(shù)的四種方式

    本文給大家介紹php解析url并得到url中的參數(shù)及獲取url參數(shù)的四種方式,涉及到將字符串參數(shù)變?yōu)閿?shù)組,將參數(shù)變?yōu)樽址南嚓P(guān)知識,本文代碼簡單易懂,感興趣的朋友一起看看吧
    2015-10-10
  • 在laravel中使用Symfony的Crawler組件分析HTML

    在laravel中使用Symfony的Crawler組件分析HTML

    這篇文章主要介紹了在laravel中使用Symfony的Crawler組件分析HTML,需要的朋友可以參考下
    2017-06-06
  • thinkphp緩存技術(shù)詳解

    thinkphp緩存技術(shù)詳解

    這篇文章主要介紹了thinkphp緩存技術(shù),詳細分析了緩存技術(shù)的特點以及在ThinkPHP中緩存的實際應(yīng)用技巧,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • PHP sdk實現(xiàn)在線打包代碼示例

    PHP sdk實現(xiàn)在線打包代碼示例

    這篇文章主要介紹了PHP sdk實現(xiàn)在線打包代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2020-12-12
  • php 實現(xiàn)銀聯(lián)商務(wù)H5支付的示例代碼

    php 實現(xiàn)銀聯(lián)商務(wù)H5支付的示例代碼

    這篇文章主要介紹了php 實現(xiàn)銀聯(lián)商務(wù)H5支付的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2019-10-10
  • WordPress中用于獲取文章作者與分類信息的方法整理

    WordPress中用于獲取文章作者與分類信息的方法整理

    這篇文章主要介紹了WordPress中用于獲取文章作者與分類信息的方法整理,都是來自于WordPress的WP_Query類之下,需要的朋友可以參考下
    2015-12-12
  • Thinkphp5.0框架的Db操作實例分析【連接、增刪改查、鏈式操作等】

    Thinkphp5.0框架的Db操作實例分析【連接、增刪改查、鏈式操作等】

    這篇文章主要介紹了Thinkphp5.0框架的Db操作,結(jié)合實例形式分析了thinkPHP5使用Db庫實現(xiàn)數(shù)據(jù)庫的連接、增刪改查及鏈式操作等相關(guān)使用技巧,需要的朋友可以參考下
    2019-10-10
  • 教你php如何實現(xiàn)驗證碼

    教你php如何實現(xiàn)驗證碼

    這篇文章教大家php如何實現(xiàn)驗證碼,驗證碼在表單實現(xiàn)越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學(xué)習了下php實現(xiàn)的驗證碼,感興趣的小伙伴們可以參考一下
    2016-01-01

最新評論