Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

87 lines
2.8KB

  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. use yii\di\Instance;
  12. use yii\helpers\VarDumper;
  13. /**
  14. * DbTarget stores log messages in a database table.
  15. *
  16. * The database connection is specified by [[db]]. Database schema could be initialized by applying migration:
  17. *
  18. * ```
  19. * yii migrate --migrationPath=@yii/log/migrations/
  20. * ```
  21. *
  22. * If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.
  23. *
  24. * You may change the name of the table used to store the data by setting [[logTable]].
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @since 2.0
  28. */
  29. class DbTarget extends Target
  30. {
  31. /**
  32. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
  33. * After the DbTarget object is created, if you want to change this property, you should only assign it
  34. * with a DB connection object.
  35. * Starting from version 2.0.2, this can also be a configuration array for creating the object.
  36. */
  37. public $db = 'db';
  38. /**
  39. * @var string name of the DB table to store cache content. Defaults to "log".
  40. */
  41. public $logTable = '{{%log}}';
  42. /**
  43. * Initializes the DbTarget component.
  44. * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
  45. * @throws InvalidConfigException if [[db]] is invalid.
  46. */
  47. public function init()
  48. {
  49. parent::init();
  50. $this->db = Instance::ensure($this->db, Connection::className());
  51. }
  52. /**
  53. * Stores log messages to DB.
  54. */
  55. public function export()
  56. {
  57. $tableName = $this->db->quoteTableName($this->logTable);
  58. $sql = "INSERT INTO $tableName ([[level]], [[category]], [[log_time]], [[prefix]], [[message]])
  59. VALUES (:level, :category, :log_time, :prefix, :message)";
  60. $command = $this->db->createCommand($sql);
  61. foreach ($this->messages as $message) {
  62. list($text, $level, $category, $timestamp) = $message;
  63. if (!is_string($text)) {
  64. // exceptions may not be serializable if in the call stack somewhere is a Closure
  65. if ($text instanceof \Throwable || $text instanceof \Exception) {
  66. $text = (string) $text;
  67. } else {
  68. $text = VarDumper::export($text);
  69. }
  70. }
  71. $command->bindValues([
  72. ':level' => $level,
  73. ':category' => $category,
  74. ':log_time' => $timestamp,
  75. ':prefix' => $this->getMessagePrefix($message),
  76. ':message' => $text,
  77. ])->execute();
  78. }
  79. }
  80. }