/** * {@inheritdoc} */ public function log($level, $message, array $context = array()) { // Remove any backtraces since they may contain an unserializable variable. unset($context['backtrace']); // Convert PSR3-style messages to SafeMarkup::format() style, so they can be // translated too in runtime. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); try { $this->connection->insert('watchdog')->fields(array('uid' => $context['uid'], 'type' => Unicode::substr($context['channel'], 0, 64), 'message' => $message, 'variables' => serialize($message_placeholders), 'severity' => $level, 'link' => $context['link'], 'location' => $context['request_uri'], 'referer' => $context['referer'], 'hostname' => Unicode::substr($context['ip'], 0, 128), 'timestamp' => $context['timestamp']))->execute(); } catch (\Exception $e) { // When running Drupal on MySQL or MariaDB you can run into several errors // that corrupt the database connection. Some examples for these kind of // errors on the database layer are "1100 - Table 'xyz' was not locked // with LOCK TABLES" and "1153 - Got a packet bigger than // 'max_allowed_packet' bytes". If such an error happens, the MySQL server // invalidates the connection and answers all further requests in this // connection with "2006 - MySQL server had gone away". In that case the // insert statement above results in a database exception. To ensure that // the causal error is written to the log we try once to open a dedicated // connection and write again. if (($e instanceof DatabaseException || $e instanceof \PDOException) && $this->connection->getTarget() != self::DEDICATED_DBLOG_CONNECTION_TARGET) { // Open a dedicated connection for logging. $key = $this->connection->getKey(); $info = Database::getConnectionInfo($key); Database::addConnectionInfo($key, self::DEDICATED_DBLOG_CONNECTION_TARGET, $info['default']); $this->connection = Database::getConnection(self::DEDICATED_DBLOG_CONNECTION_TARGET, $key); // Now try once to log the error again. $this->log($level, $message, $context); } else { throw $e; } } }
/** * {@inheritdoc} */ public function log($level, $message, array $context = array()) { // Translate the RFC logging levels into their Drush counterparts, more or // less. // @todo ALERT, CRITICAL and EMERGENCY are considered show-stopping errors, // and they should cause Drush to exit or panic. Not sure how to handle this, // though. switch ($level) { case RfcLogLevel::ALERT: case RfcLogLevel::CRITICAL: case RfcLogLevel::EMERGENCY: case RfcLogLevel::ERROR: $error_type = 'error'; break; case RfcLogLevel::WARNING: $error_type = 'warning'; break; case RfcLogLevel::DEBUG: case RfcLogLevel::INFO: case RfcLogLevel::NOTICE: $error_type = 'notice'; break; default: $error_type = $level; break; } // Populate the message placeholders and then replace them in the message. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders); drush_log($message, $error_type); }
/** * {@inheritdoc} */ public function log($level, $message, array $context = array()) { // Remove any backtraces since they may contain an unserializable variable. unset($context['backtrace']); // Convert PSR3-style messages to String::format() style, so they can be // translated too in runtime. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); $this->database->insert('watchdog')->fields(array('uid' => $context['uid'], 'type' => substr($context['channel'], 0, 64), 'message' => $message, 'variables' => serialize($message_placeholders), 'severity' => $level, 'link' => substr($context['link'], 0, 255), 'location' => $context['request_uri'], 'referer' => $context['referer'], 'hostname' => substr($context['ip'], 0, 128), 'timestamp' => $context['timestamp']))->execute(); }
/** * {@inheritdoc} */ public function log($level, $message, array $context = array()) { global $base_url; // Ensure we have a connection available. $this->openConnection(); // Populate the message placeholders and then replace them in the message. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders); $entry = strtr($this->config->get('format'), array('!base_url' => $base_url, '!timestamp' => $context['timestamp'], '!type' => $context['channel'], '!ip' => $context['ip'], '!request_uri' => $context['request_uri'], '!referer' => $context['referer'], '!uid' => $context['uid'], '!link' => strip_tags($context['link']), '!message' => strip_tags($message))); syslog($level, $entry); }
/** * {@inheritdoc} * * @todo: create a TypedData logger-entry object: https://www.drupal.org/node/2625238 */ public function log($level, $message, array $context = []) { // Remove any backtraces since they may contain an unserializable variable. unset($context['backtrace']); // Convert PSR3-style messages to SafeMarkup::format() style, so they can be // translated too in runtime. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); $logger_entry = ['uid' => $context['uid'], 'type' => $context['channel'], 'message' => $message, 'variables' => $message_placeholders, 'severity' => $level, 'link' => $context['link'], 'location' => $context['request_uri'], 'referer' => $context['referer'], 'hostname' => $context['ip'], 'timestamp' => $context['timestamp']]; // Dispatch logger_entry event. $event = new SystemLoggerEvent($logger_entry, ['logger_entry' => $logger_entry]); $this->dispatcher->dispatch(SystemLoggerEvent::EVENT_NAME, $event); }
/** * {@inheritdoc} */ public function log($level, $message, array $context = array()) { // Translate the RFC logging levels into their Drush counterparts, more or // less. // @todo ALERT, CRITICAL and EMERGENCY are considered show-stopping errors, // and they should cause Drush to exit or panic. Not sure how to handle this, // though. switch ($level) { case RfcLogLevel::ALERT: case RfcLogLevel::CRITICAL: case RfcLogLevel::EMERGENCY: case RfcLogLevel::ERROR: $error_type = LogLevel::ERROR; break; case RfcLogLevel::WARNING: $error_type = LogLevel::WARNING; break; // TODO: RfcLogLevel::DEBUG should be 'debug' rather than 'notice'? // TODO: RfcLogLevel::DEBUG should be 'debug' rather than 'notice'? case RfcLogLevel::DEBUG: case RfcLogLevel::INFO: case RfcLogLevel::NOTICE: $error_type = LogLevel::NOTICE; break; // TODO: Unknown log levels that are not defined // in Psr\Log\LogLevel or Drush\Log\LogLevel SHOULD NOT be used. See // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md // We should convert these to 'notice'. // TODO: Unknown log levels that are not defined // in Psr\Log\LogLevel or Drush\Log\LogLevel SHOULD NOT be used. See // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md // We should convert these to 'notice'. default: $error_type = $level; break; } // Populate the message placeholders and then replace them in the message. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); $message = empty($message_placeholders) ? $message : strtr($message, $message_placeholders); $this->logger->log($error_type, $message, $context); }