5 * Contains \Drupal\Console\Command\Database\DatabaseLogBase.
8 namespace Drupal\Console\Command\Database;
10 use Drupal\Core\Database\Connection;
11 use Drupal\Core\Datetime\DateFormatterInterface;
12 use Drupal\Core\Entity\EntityTypeManagerInterface;
13 use Drupal\Core\StringTranslation\TranslatableMarkup;
14 use Drupal\Core\StringTranslation\TranslationInterface;
15 use Drupal\user\UserStorageInterface;
16 use Drupal\Console\Core\Command\Command;
17 use Drupal\Core\Logger\RfcLogLevel;
18 use Drupal\user\Entity\User;
19 use Drupal\Component\Utility\Unicode;
20 use Drupal\Component\Utility\Html;
21 use Symfony\Component\Console\Input\InputOption;
22 use Symfony\Component\Console\Input\InputInterface;
25 * Class DatabaseLogBase
27 * @package Drupal\Console\Command\Database
29 abstract class DatabaseLogBase extends Command
37 * @var DateFormatterInterface
39 protected $dateFormatter;
42 * @var EntityTypeManagerInterface
44 protected $entityTypeManager;
47 * @var TranslationInterface
49 protected $stringTranslation;
52 * @var UserStorageInterface
54 protected $userStorage;
57 * @var TranslatableMarkup[]
59 protected $severityList;
69 protected $eventSeverity;
77 * LogDebugCommand constructor.
79 * @param Connection $database
80 * @param DateFormatterInterface $dateFormatter
81 * @param EntityTypeManagerInterface $entityTypeManager
82 * @param TranslationInterface $stringTranslation
84 public function __construct(
86 DateFormatterInterface $dateFormatter,
87 EntityTypeManagerInterface $entityTypeManager,
88 TranslationInterface $stringTranslation
90 $this->database = $database;
91 $this->dateFormatter = $dateFormatter;
92 $this->entityTypeManager = $entityTypeManager;
93 $this->stringTranslation = $stringTranslation;
94 $this->userStorage = $this->entityTypeManager->getStorage('user');
95 $this->severityList = RfcLogLevel::getLevels();
96 parent::__construct();
100 * addDefaultLoggingOptions.
102 protected function addDefaultLoggingOptions()
108 InputOption::VALUE_OPTIONAL,
109 $this->trans('commands.database.log.common.options.type')
114 InputOption::VALUE_OPTIONAL,
115 $this->trans('commands.database.log.common.options.severity')
120 InputOption::VALUE_OPTIONAL,
121 $this->trans('commands.database.log.common.options.user-id')
126 * @param InputInterface $input
128 protected function getDefaultOptions(InputInterface $input)
130 $this->eventType = $input->getOption('type');
131 $this->eventSeverity = $input->getOption('severity');
132 $this->userId = $input->getOption('user-id');
136 * @param null $offset
138 * @return bool|\Drupal\Core\Database\Query\SelectInterface
140 protected function makeQuery($offset = null, $range = 1000)
142 $query = $this->database->select('watchdog', 'w');
156 if ($this->eventType) {
157 $query->condition('type', $this->eventType);
160 if ($this->eventSeverity) {
161 if (!in_array($this->eventSeverity, $this->severityList)) {
162 $this->getIo()->error(
164 $this->trans('database.log.common.messages.invalid-severity'),
173 $this->eventSeverity,
180 $query->condition('uid', $this->userId);
183 $query->orderBy('wid', 'ASC');
186 $query->range($offset, $range);
193 * Generic logging table header
197 protected function createTableHeader()
200 $this->trans('commands.database.log.common.messages.event-id'),
201 $this->trans('commands.database.log.common.messages.type'),
202 $this->trans('commands.database.log.common.messages.date'),
203 $this->trans('commands.database.log.common.messages.message'),
204 $this->trans('commands.database.log.common.messages.user'),
205 $this->trans('commands.database.log.common.messages.severity'),
210 * @param \stdClass $dblog
213 protected function createTableRow(\stdClass $dblog)
219 $user = $this->userStorage->load($dblog->uid);
224 $this->dateFormatter->format($dblog->timestamp, 'short'),
226 Html::decodeEntities(strip_tags($this->formatMessage($dblog))),
231 $user->getUsername() . ' (' . $user->id() . ')',
232 $this->severityList[$dblog->severity]->render(),
237 * Formats a database log message.
240 * The record from the watchdog table. The object properties are: wid, uid,
241 * severity, type, timestamp, message, variables, link, name.
243 * @return string|false
244 * The formatted log message or FALSE if the message or variables properties
247 protected function formatMessage(\stdClass $event)
251 // Check for required properties.
252 if (isset($event->message, $event->variables)) {
253 // Messages without variables or user specified text.
254 if ($event->variables === 'N;') {
255 return $event->message;
258 return $this->stringTranslation->translate(
260 unserialize($event->variables)
271 protected function formatSingle($dblog)
273 return array_combine(
274 $this->createTableHeader(),
275 $this->createTableRow($dblog)