5 * Contains \Drupal\Console\Command\Database\DatabaseLogBase.
8 namespace Drupal\Console\Command\Database;
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Drupal\Core\Database\Connection;
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Entity\EntityTypeManagerInterface;
14 use Drupal\Core\StringTranslation\TranslatableMarkup;
15 use Drupal\Core\StringTranslation\TranslationInterface;
16 use Drupal\user\UserStorageInterface;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Core\Logger\RfcLogLevel;
19 use Drupal\user\Entity\User;
20 use Drupal\Component\Utility\Unicode;
21 use Drupal\Component\Utility\Html;
22 use Symfony\Component\Console\Input\InputOption;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Drupal\Console\Core\Style\DrupalStyle;
27 * Class DatabaseLogBase
29 * @package Drupal\Console\Command\Database
31 abstract class DatabaseLogBase extends Command
41 * @var DateFormatterInterface
43 protected $dateFormatter;
46 * @var EntityTypeManagerInterface
48 protected $entityTypeManager;
51 * @var TranslationInterface
53 protected $stringTranslation;
56 * @var UserStorageInterface
58 protected $userStorage;
61 * @var TranslatableMarkup[]
63 protected $severityList;
73 protected $eventSeverity;
81 * LogDebugCommand constructor.
83 * @param Connection $database
84 * @param DateFormatterInterface $dateFormatter
85 * @param EntityTypeManagerInterface $entityTypeManager
86 * @param TranslationInterface $stringTranslation
88 public function __construct(
90 DateFormatterInterface $dateFormatter,
91 EntityTypeManagerInterface $entityTypeManager,
92 TranslationInterface $stringTranslation
94 $this->database = $database;
95 $this->dateFormatter = $dateFormatter;
96 $this->entityTypeManager = $entityTypeManager;
97 $this->stringTranslation = $stringTranslation;
98 $this->userStorage = $this->entityTypeManager->getStorage('user');
99 $this->severityList = RfcLogLevel::getLevels();
100 parent::__construct();
104 * addDefaultLoggingOptions.
106 protected function addDefaultLoggingOptions()
112 InputOption::VALUE_OPTIONAL,
113 $this->trans('commands.database.log.common.options.type')
118 InputOption::VALUE_OPTIONAL,
119 $this->trans('commands.database.log.common.options.severity')
124 InputOption::VALUE_OPTIONAL,
125 $this->trans('commands.database.log.common.options.user-id')
130 * @param InputInterface $input
132 protected function getDefaultOptions(InputInterface $input)
134 $this->eventType = $input->getOption('type');
135 $this->eventSeverity = $input->getOption('severity');
136 $this->userId = $input->getOption('user-id');
140 * @param DrupalStyle $io
141 * @param null $offset
143 * @return bool|\Drupal\Core\Database\Query\SelectInterface
145 protected function makeQuery(DrupalStyle $io, $offset = null, $range = 1000)
147 $query = $this->database->select('watchdog', 'w');
161 if ($this->eventType) {
162 $query->condition('type', $this->eventType);
165 if ($this->eventSeverity) {
166 if (!in_array($this->eventSeverity, $this->severityList)) {
169 $this->trans('database.log.common.messages.invalid-severity'),
178 $this->eventSeverity,
185 $query->condition('uid', $this->userId);
188 $query->orderBy('wid', 'ASC');
191 $query->range($offset, $range);
198 * Generic logging table header
202 protected function createTableHeader()
205 $this->trans('commands.database.log.common.messages.event-id'),
206 $this->trans('commands.database.log.common.messages.type'),
207 $this->trans('commands.database.log.common.messages.date'),
208 $this->trans('commands.database.log.common.messages.message'),
209 $this->trans('commands.database.log.common.messages.user'),
210 $this->trans('commands.database.log.common.messages.severity'),
215 * @param \stdClass $dblog
218 protected function createTableRow(\stdClass $dblog)
224 $user = $this->userStorage->load($dblog->uid);
229 $this->dateFormatter->format($dblog->timestamp, 'short'),
231 Html::decodeEntities(strip_tags($this->formatMessage($dblog))),
236 $user->getUsername() . ' (' . $user->id() . ')',
237 $this->severityList[$dblog->severity]->render(),
242 * Formats a database log message.
245 * The record from the watchdog table. The object properties are: wid, uid,
246 * severity, type, timestamp, message, variables, link, name.
248 * @return string|false
249 * The formatted log message or FALSE if the message or variables properties
252 protected function formatMessage(\stdClass $event)
256 // Check for required properties.
257 if (isset($event->message, $event->variables)) {
258 // Messages without variables or user specified text.
259 if ($event->variables === 'N;') {
260 return $event->message;
263 return $this->stringTranslation->translate(
265 unserialize($event->variables)
276 protected function formatSingle($dblog)
278 return array_combine(
279 $this->createTableHeader(),
280 $this->createTableRow($dblog)