Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2389 [database:log:poll] Add a command to poll the db logging for dev environments #2529

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/services/drupal-console/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ services:
arguments: ['@console.redbean', '@database']
tags:
- { name: console.command }
console.database_log_debug:
class: Drupal\Console\Command\Database\LogDebugCommand
tags:
- { name: console.command }
console.database_log_poll:
class: Drupal\Console\Command\Database\LogPollCommand
tags:
- { name: console.command }
14 changes: 14 additions & 0 deletions config/translations/en/database.log.poll.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description: 'Poll current log events for the application'
options:
type: 'Filter events by a specific type'
severity: 'Filter events by a specific level of severity'
user-id: 'Filter events by a specific user id'
duration: 'Duration between polling in seconds, default 30'
messages:
event-id: 'Event ID'
type: Type
date: Date
message: Message
user: User
severity: Severity
invalid-severity: 'Severity type is invalid, filter was ignored'
29 changes: 5 additions & 24 deletions src/Command/Database/LogClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LogClearCommand extends Command
{
use CommandTrait;

use LogCommandTrait;

/**
* @var Connection
*/
Expand All @@ -42,30 +44,9 @@ protected function configure()
{
$this
->setName('database:log:clear')
->setDescription($this->trans('commands.database.log.clear.description'))
->addArgument(
'event-id',
InputArgument::OPTIONAL,
$this->trans('commands.database.log.clear.arguments.event-id')
)
->addOption(
'type',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.log.clear.options.type')
)
->addOption(
'severity',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.log.clear.options.severity')
)
->addOption(
'user-id',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.log.clear.options.user-id')
);
->setDescription($this->trans('commands.database.log.clear.description'));

LogCommandTrait::addBasicLoggingConfiguration($this);
}

/**
Expand Down
149 changes: 149 additions & 0 deletions src/Command/Database/LogCommandBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Created by PhpStorm.
* User: twhiston
* Date: 21/09/16
* Time: 11:08
*/

namespace Drupal\Console\Command\Database;

use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
use Drupal\Console\Command\Shared\CommandTrait;

class LogCommandBase extends Command {

use CommandTrait;

use ContainerAwareCommandTrait;

use LogCommandTrait;


/**
* @var Connection
*/
protected $database;

/**
* @var DateFormatterInterface
*/
protected $dateFormatter;

/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var TranslatorInterface
*/
protected $stringTranslation;

/**
* LogDebugCommand constructor.
* @param Connection $database
* @param DateFormatterInterface $dateFormatter
* @param EntityTypeManagerInterface $entityTypeManager
* @param TranslatorInterface $stringTranslation
*/
public function __construct(
Connection $database,
DateFormatterInterface $dateFormatter,
EntityTypeManagerInterface $entityTypeManager,
TranslatorInterface $stringTranslation
) {
$this->database = $database;
$this->dateFormatter = $dateFormatter;
$this->entityTypeManager = $entityTypeManager;
$this->stringTranslation = $stringTranslation;
parent::__construct();
}

public function addBasicLoggingConfiguration() {
$this
->addArgument(
'event-id',
InputArgument::OPTIONAL,
$this->trans('commands.database.log.debug.arguments.event-id')
)
->addOption(
'type',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.log.debug.options.type')
)
->addOption(
'severity',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.log.debug.options.severity')
)
->addOption(
'user-id',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.log.debug.options.user-id')
);
}

protected function createTableHeader()
{
return [
$this->trans('commands.database.log.debug.messages.event-id'),
$this->trans('commands.database.log.debug.messages.type'),
$this->trans('commands.database.log.debug.messages.date'),
$this->trans('commands.database.log.debug.messages.message'),
$this->trans('commands.database.log.debug.messages.user'),
$this->trans('commands.database.log.debug.messages.severity'),
];
}

protected function createTableRow($dblog, $userStorage, $dateFormatter, $severity) {

$user = $userStorage->load($dblog->uid);

return [
$dblog->wid,
$dblog->type,
$dateFormatter->format($dblog->timestamp, 'short'),
Unicode::truncate(Html::decodeEntities(strip_tags($this->formatMessage($dblog))), 500, TRUE, TRUE),
$user->getUsername() . ' (' . $user->id() . ')',
$severity[$dblog->severity]
];
}


/**
* 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.
*/
public function formatMessage($event) {
$message = FALSE;

// Check for required properties.
if (isset($event->message) && isset($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;
}

}
Loading