DebugCommands.php in Purge 8.3
File
modules/purge_drush/src/Commands/DebugCommands.php
View source
<?php
namespace Drupal\purge_drush\Commands;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\purge\Logger\LoggerServiceInterface;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
class DebugCommands extends DrushCommands {
protected $purgeLogger;
public function __construct(LoggerServiceInterface $purge_logger) {
parent::__construct();
$this->purgeLogger = $purge_logger;
}
public function debugDisable(array $options = [
'format' => 'string',
]) {
$enabled_channels = function () {
$ids = [];
foreach ($this->purgeLogger
->getChannels() as $channel) {
if (in_array(RfcLogLevel::DEBUG, $channel['grants'])) {
$ids[] = $channel['id'];
}
}
return $ids;
};
if (empty($enabled_channels())) {
throw new \Exception(dt("Debugging already disabled for all channels."));
}
if ($options['format'] == 'string') {
$this
->io()
->writeln(dt("Disabled debug logging for the following log channels:"));
$this
->io()
->listing($enabled_channels());
}
foreach ($this->purgeLogger
->getChannels() as $channel) {
if (in_array(RfcLogLevel::DEBUG, $channel['grants'])) {
$key = array_search(RfcLogLevel::DEBUG, $channel['grants']);
unset($channel['grants'][$key]);
$this->purgeLogger
->setChannel($channel['id'], $channel['grants']);
}
}
}
public function debugEnable(array $options = [
'format' => 'string',
]) {
$disabled_channels = function () {
$ids = [];
foreach ($this->purgeLogger
->getChannels() as $channel) {
if (!in_array(RfcLogLevel::DEBUG, $channel['grants'])) {
$ids[] = $channel['id'];
}
}
return $ids;
};
$enable = function () {
foreach ($this->purgeLogger
->getChannels() as $channel) {
if (!in_array(RfcLogLevel::DEBUG, $channel['grants'])) {
$channel['grants'][] = RfcLogLevel::DEBUG;
$this->purgeLogger
->setChannel($channel['id'], $channel['grants']);
}
}
};
if (empty($disabled_channels())) {
throw new \Exception(dt("Debugging already enabled for all channels."));
}
if ($options['format'] == 'string') {
$this
->output()
->writeln(dt("About to enable debugging for the following log channels:"));
$this
->io()
->listing($disabled_channels());
$this
->io()
->caution(dt("Once enabled, this allows you to run Drush commands like" . " p:queue-work with the -d parameter, giving you a detailed" . " amount of live-debugging information getting logged by Purge" . " and modules integrating with it." . " HOWEVER, debug logging is VERY verbose and can add" . " millions of messages when left enabled for too long. NEVER" . " enable this on a production environment without fully" . " understanding the consequences!"));
if ($this
->io()
->confirm(dt("Are you sure you want to enable it?"))) {
$enable();
$this
->output()
->writeln(dt("Enabled! Use p:debug-dis to disable when you're finished!"));
}
else {
throw new UserAbortException();
}
}
else {
$enable();
}
}
}