|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
-
-
- namespace yii\log;
-
- use Yii;
- use yii\db\Connection;
- use yii\base\InvalidConfigException;
- use yii\di\Instance;
- use yii\helpers\VarDumper;
-
-
- class DbTarget extends Target
- {
-
-
- public $db = 'db';
-
-
- public $logTable = '{{%log}}';
-
-
-
-
- public function init()
- {
- parent::init();
- $this->db = Instance::ensure($this->db, Connection::className());
- }
-
-
-
- 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)) {
-
- if ($text instanceof \Throwable || $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();
- }
- }
- }
|