Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DbTarget.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $text = VarDumper::export($text);
  65. }
  66. $command->bindValues([
  67. ':level' => $level,
  68. ':category' => $category,
  69. ':log_time' => $timestamp,
  70. ':prefix' => $this->getMessagePrefix($message),
  71. ':message' => $text,
  72. ])->execute();
  73. }
  74. }
  75. }