一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - Yii使用DbTarget實現日志功能的示例代碼

Yii使用DbTarget實現日志功能的示例代碼

2021-10-20 13:28huaweichenai PHP教程

這篇文章主要介紹了Yii使用DbTarget實現日志功能的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

Yii使用DbTarget實現日志功能的示例代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'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'],//日志等級
  ],
 ],
],

二:生成日志表

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

?
1
php yii migrate --migrationpath=@yii/log/migrations/

三:使用日志

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

?
1
yii::info()

四:自定義dbtarget日志

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

(1)在項目目錄下執行

?
1
php yii migrate/create create_test_log

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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日志表

?
1
php yii migrate

2:編寫一個dbtarget類來繼承yiilogdbtarget類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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類改成我們自定義的類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'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來記錄日志

到此這篇關于yii使用dbtarget實現日志功能的示例代碼的文章就介紹到這了,更多相關yii dbtarget 日志內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://segmentfault.com/a/1190000023313633

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 白丝女仆被啪到深夜漫画 | 免费亚洲成人 | 欧美一区二区三区视视频 | 午夜dj影院在线视频观看完整 | 青青青在线观看国产精品 | 精品国产理论在线观看不卡 | 国产盗摄wc女厕所 | 亚洲2017天堂色无码 | 亚洲成人影院在线观看 | 911香蕉视频| 农村妇女野外性生话免费视频 | 羞羞污视频 | 女人和拘做受全过程免费 | 91混血大战上海双胞胎 | 99视频久久 | 亚洲国产成人久久99精品 | 美女被吸乳得到大胸 | 国产欧美日韩不卡一区二区三区 | 26uuu成人人网图片 | 四虎影院免费在线 | 99视频全部免费 | 国产福利视频一区二区微拍 | 色综合久久日韩国产 | 欧美午夜精品 | 日本视频高清免费观看xxx | 日本免费三片在线观看 | 调教小荡娃h | www.四虎在线 | 天天操精品 | 性夜影院爽黄A爽免费动漫 性色欲情网站IWWW九文堂 | 色老板免费| 国产特级 | 国产中文在线 | 国产一区二区免费视频 | ffyybb免费福利视频 | 国产伦码精品一区二区三区 | 日韩xx00 | 日韩一级在线观看 | 兽皇日本 | 高清不卡免费一区二区三区 | 国产精品吹潮香蕉在线观看 |