database = $database; $this->dateFormatter = $dateFormatter; $this->entityTypeManager = $entityTypeManager; $this->stringTranslation = $stringTranslation; $this->userStorage = $this->entityTypeManager->getStorage('user'); $this->severityList = RfcLogLevel::getLevels(); parent::__construct(); } /** * addDefaultLoggingOptions. */ protected function addDefaultLoggingOptions() { $this ->addOption( 'type', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.database.log.common.options.type') ) ->addOption( 'severity', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.database.log.common.options.severity') ) ->addOption( 'user-id', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.database.log.common.options.user-id') ); } /** * @param InputInterface $input */ protected function getDefaultOptions(InputInterface $input) { $this->eventType = $input->getOption('type'); $this->eventSeverity = $input->getOption('severity'); $this->userId = $input->getOption('user-id'); } /** * @param null $offset * @param int $range * @return bool|\Drupal\Core\Database\Query\SelectInterface */ protected function makeQuery($offset = null, $range = 1000) { $query = $this->database->select('watchdog', 'w'); $query->fields( 'w', [ 'wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', ] ); if ($this->eventType) { $query->condition('type', $this->eventType); } if ($this->eventSeverity) { if (!in_array($this->eventSeverity, $this->severityList)) { $this->getIo()->error( sprintf( $this->trans('database.log.common.messages.invalid-severity'), $this->eventSeverity ) ); return false; } $query->condition( 'severity', array_search( $this->eventSeverity, $this->severityList ) ); } if ($this->userId) { $query->condition('uid', $this->userId); } $query->orderBy('wid', 'ASC'); if ($offset) { $query->range($offset, $range); } return $query; } /** * Generic logging table header * * @return array */ protected function createTableHeader() { return [ $this->trans('commands.database.log.common.messages.event-id'), $this->trans('commands.database.log.common.messages.type'), $this->trans('commands.database.log.common.messages.date'), $this->trans('commands.database.log.common.messages.message'), $this->trans('commands.database.log.common.messages.user'), $this->trans('commands.database.log.common.messages.severity'), ]; } /** * @param \stdClass $dblog * @return array */ protected function createTableRow(\stdClass $dblog) { /** * @var User $user */ $user = $this->userStorage->load($dblog->uid); return [ $dblog->wid, $dblog->type, $this->dateFormatter->format($dblog->timestamp, 'short'), Unicode::truncate( Html::decodeEntities(strip_tags($this->formatMessage($dblog))), 500, true, true ), $user->getUsername() . ' (' . $user->id() . ')', $this->severityList[$dblog->severity]->render(), ]; } /** * Formats a database log message. * * @param $event * The record from the watchdog table. The object properties are: wid, uid, * severity, type, timestamp, message, variables, link, name. * * @return string|false * The formatted log message or FALSE if the message or variables properties * are not set. */ protected function formatMessage(\stdClass $event) { $message = false; // Check for required properties. if (isset($event->message, $event->variables)) { // Messages without variables or user specified text. if ($event->variables === 'N;') { return $event->message; } return $this->stringTranslation->translate( $event->message, unserialize($event->variables) ); } return $message; } /** * @param $dblog * @return array */ protected function formatSingle($dblog) { return array_combine( $this->createTableHeader(), $this->createTableRow($dblog) ); } }