DbTarget.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\log;
  8. use Yii;
  9. use yii\db\Connection;
  10. use yii\base\InvalidConfigException;
  11. /**
  12. * DbTarget stores log messages in a database table.
  13. *
  14. * By default, DbTarget stores the log messages in a DB table named 'tbl_log'. This table
  15. * must be pre-created. The table name can be changed by setting [[logTable]].
  16. *
  17. * @author Qiang Xue <[email protected]>
  18. * @since 2.0
  19. */
  20. class DbTarget extends Target
  21. {
  22. /**
  23. * @var Connection|string the DB connection object or the application component ID of the DB connection.
  24. * After the DbTarget object is created, if you want to change this property, you should only assign it
  25. * with a DB connection object.
  26. */
  27. public $db = 'db';
  28. /**
  29. * @var string name of the DB table to store cache content.
  30. * The table should be pre-created as follows:
  31. *
  32. * ~~~
  33. * CREATE TABLE tbl_log (
  34. * id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  35. * level INTEGER,
  36. * category VARCHAR(255),
  37. * log_time INTEGER,
  38. * message TEXT,
  39. * INDEX idx_log_level (level),
  40. * INDEX idx_log_category (category)
  41. * )
  42. * ~~~
  43. *
  44. * Note that the 'id' column must be created as an auto-incremental column.
  45. * The above SQL uses the MySQL syntax. If you are using other DBMS, you need
  46. * to adjust it accordingly. For example, in PostgreSQL, it should be `id SERIAL PRIMARY KEY`.
  47. *
  48. * The indexes declared above are not required. They are mainly used to improve the performance
  49. * of some queries about message levels and categories. Depending on your actual needs, you may
  50. * want to create additional indexes (e.g. index on `log_time`).
  51. */
  52. public $logTable = '{{%log}}';
  53. /**
  54. * Initializes the DbTarget component.
  55. * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
  56. * @throws InvalidConfigException if [[db]] is invalid.
  57. */
  58. public function init()
  59. {
  60. parent::init();
  61. if (is_string($this->db)) {
  62. $this->db = Yii::$app->getComponent($this->db);
  63. }
  64. if (!$this->db instanceof Connection) {
  65. throw new InvalidConfigException("DbTarget::db must be either a DB connection instance or the application component ID of a DB connection.");
  66. }
  67. }
  68. /**
  69. * Stores log messages to DB.
  70. */
  71. public function export()
  72. {
  73. $tableName = $this->db->quoteTableName($this->logTable);
  74. $sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[message]])
  75. VALUES (:level, :category, :log_time, :message)";
  76. $command = $this->db->createCommand($sql);
  77. foreach ($this->messages as $message) {
  78. $command->bindValues([
  79. ':level' => $message[1],
  80. ':category' => $message[2],
  81. ':log_time' => $message[3],
  82. ':message' => $message[0],
  83. ])->execute();
  84. }
  85. }
  86. }