Blame view

vendor/yiisoft/yii2/log/DbTarget.php 2.8 KB
abf1649b   andryeyev   Чистая установка ...
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
  <?php
  /**
   * @link http://www.yiiframework.com/
   * @copyright Copyright (c) 2008 Yii Software LLC
   * @license http://www.yiiframework.com/license/
   */
  
  namespace yii\log;
  
  use Yii;
  use yii\db\Connection;
  use yii\base\InvalidConfigException;
  use yii\di\Instance;
  use yii\helpers\VarDumper;
  
  /**
   * DbTarget stores log messages in a database table.
   *
   * The database connection is specified by [[db]]. Database schema could be initialized by applying migration:
   *
   * ```
   * yii migrate --migrationPath=@yii/log/migrations/
   * ```
   *
   * If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.
   *
   * You may change the name of the table used to store the data by setting [[logTable]].
   *
   * @author Qiang Xue <qiang.xue@gmail.com>
   * @since 2.0
   */
  class DbTarget extends Target
  {
      /**
       * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
       * After the DbTarget object is created, if you want to change this property, you should only assign it
       * with a DB connection object.
       * Starting from version 2.0.2, this can also be a configuration array for creating the object.
       */
      public $db = 'db';
      /**
       * @var string name of the DB table to store cache content. Defaults to "log".
       */
      public $logTable = '{{%log}}';
  
  
      /**
       * Initializes the DbTarget component.
       * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
       * @throws InvalidConfigException if [[db]] is invalid.
       */
      public function init()
      {
          parent::init();
          $this->db = Instance::ensure($this->db, Connection::className());
      }
  
      /**
       * Stores log messages to DB.
       */
      public function export()
      {
          $tableName = $this->db->quoteTableName($this->logTable);
          $sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]])
                  VALUES (:level, :category, :log_time, :prefix, :message)";
          $command = $this->db->createCommand($sql);
          foreach ($this->messages as $message) {
              list($text, $level, $category, $timestamp) = $message;
              if (!is_string($text)) {
                  // exceptions may not be serializable if in the call stack somewhere is a Closure
                  if ($text instanceof \Exception) {
                      $text = (string) $text;
                  } else {
                      $text = VarDumper::export($text);
                  }
              }
              $command->bindValues([
                  ':level' => $level,
                  ':category' => $category,
                  ':log_time' => $timestamp,
                  ':prefix' => $this->getMessagePrefix($message),
                  ':message' => $text,
              ])->execute();
          }
      }
  }