Example #1
0
 /**
  * Сохраняет сообщения лога в базу данных.
  */
 public function export()
 {
     $tableName = $this->db->quoteTableName($this->logTable);
     $sql = "INSERT INTO {$tableName} ([[level]], [[category]], [[log_time]], [[prefix]], [[message]])\n                VALUES (:level, :category, :log_time, :prefix, :message)";
     $command = $this->db->createCommand($sql);
     $i = 1;
     $saveText = '';
     foreach ($this->messages as $message) {
         if ($i == 1) {
             list($text, $level, $category, $timestamp) = $message;
         } else {
             $text = $message[0];
         }
         if (!is_string($text)) {
             // exceptions may not be serializable if in the call stack somewhere is a Closure
             if ($text instanceof \Exception) {
                 $saveText .= (string) $text . "\n\n";
             } else {
                 $saveText .= VarDumper::export($text) . "\n\n";
             }
         } else {
             $saveText .= $text;
         }
         $i++;
     }
     $command->bindValues([':level' => $level, ':category' => $category, ':log_time' => $timestamp, ':prefix' => $this->getMessagePrefix($message), ':message' => $saveText])->execute();
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function formatMessage($message)
 {
     list($text, $level, $category, $timestamp) = $message;
     $level = Logger::getLevelName($level);
     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);
         }
     }
     $prefix = $this->getMessagePrefix($message);
     return "{$prefix}[{$level}][{$category}] {$text}";
 }
Example #3
0
 /**
  * Stores log messages to DB.
  */
 public function export()
 {
     $tableName = $this->db->quoteTableName($this->logTable);
     $sql = "INSERT INTO {$tableName} ([[level]], [[category]], [[log_time]], [[prefix]], [[message]], [[user_id]], [[book_id]])\n                VALUES (:level, :category, :log_time, :prefix, :message, :user_id, :book_id)";
     $command = $this->db->createCommand($sql);
     foreach ($this->messages as $message) {
         list($text, $level, $category, $timestamp) = $message;
         if (!is_string($text)) {
             $text = VarDumper::export($text);
         }
         if ($category === 'book') {
             $m = explode('_', $text);
             $book_id = $m[0];
             $text = end($m);
         }
         $command->bindValues([':level' => $level, ':category' => $category, ':log_time' => $timestamp, ':prefix' => $this->getMessagePrefix($message), ':message' => $text, ':user_id' => \Yii::$app->user->id ? \Yii::$app->user->id : null, ':book_id' => isset($book_id) ? $book_id : ''])->execute();
     }
 }
Example #4
0
 /**
  * Handles uncaught PHP exceptions.
  *
  * This method is implemented as a PHP exception handler.
  *
  * @param \Exception $exception the exception that is not caught
  */
 public function handleException($exception)
 {
     // disable error capturing to avoid recursive errors while handling exceptions
     $this->unregister();
     // set preventive HTTP status code to 500 in case error handling somehow fails and headers are sent
     // HTTP exceptions will override this value in renderException()
     if (PHP_SAPI !== 'cli') {
         http_response_code(500);
     }
     try {
         $this->renderException($exception);
     } catch (\Exception $e) {
         // an other exception could be thrown while displaying the exception
         $msg = "An Error occurred while handling another error:\n";
         $msg .= (string) $e;
         $msg .= "\nPrevious exception:\n";
         $msg .= (string) $exception;
         echo 'An internal server error occurred.';
         $msg .= "\n\$_SERVER = " . VarDumper::export($_SERVER);
         error_log($msg);
         exit(1);
     }
 }
 /**
  * Generates a string depending on enableI18N property
  *
  * @param string $string the text be generated
  * @param array $placeholders the placeholders to use by `Yii::t()`
  * @return string
  */
 public function generateString($string = '', $placeholders = [])
 {
     $string = addslashes($string);
     if ($this->enableI18N) {
         // If there are placeholders, use them
         if (!empty($placeholders)) {
             $ph = ', ' . VarDumper::export($placeholders);
         } else {
             $ph = '';
         }
         $str = "Lang::get('{$this->messageCategory}.{$string}')";
     } else {
         // No I18N, replace placeholders by real words, if any
         if (!empty($placeholders)) {
             $phKeys = array_map(function ($word) {
                 return '{' . $word . '}';
             }, array_keys($placeholders));
             $phValues = array_values($placeholders);
             $str = "'" . str_replace($phKeys, $phValues, $string) . "'";
         } else {
             // No placeholders, just the given string
             $str = "'" . $string . "'";
         }
     }
     return $str;
 }
Example #6
0
 /**
  * Formats a log message for display as a string.
  * @param string $message The log message to be formatted.
  * @return string The formatted message.
  */
 public function formatMessage($message) : string
 {
     list($text, $level, $category) = $message;
     return strtr('[{level}@{category}] {text}', ['{category}' => $category, '{level}' => Logger::getLevelName($level), '{text}' => is_string($text) ? $text : VarDumper::export($text)]);
 }