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

Enable custom Logger(s). #5022

Merged
merged 6 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Commands/DrushCommands.php
Expand Up @@ -2,6 +2,7 @@

namespace Drush\Commands;

use Drush\Log\DrushLoggerManager;
use Drush\Log\Logger;
use Symfony\Component\Console\Style\SymfonyStyle;
use Consolidation\AnnotatedCommand\CommandData;
Expand Down Expand Up @@ -63,7 +64,7 @@ protected function io(): SymfonyStyle
/**
* Returns a logger object.
*/
protected function logger(): ?Logger
protected function logger(): ?DrushLoggerManager
{
return $this->logger;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Log/DrushLoggerManager.php
@@ -0,0 +1,14 @@
<?php

namespace Drush\Log;

use Consolidation\Log\ConsoleLogLevel;
use Consolidation\Log\LoggerManager;

class DrushLoggerManager extends LoggerManager implements SuccessInterface
{
public function success(string $message, array $context = array())
{
$this->log(ConsoleLogLevel::SUCCESS, $message, $context);
}
}
8 changes: 1 addition & 7 deletions src/Log/Logger.php
Expand Up @@ -20,12 +20,11 @@

namespace Drush\Log;

use Consolidation\Log\ConsoleLogLevel;
use Drush\Drush;
use Robo\Log\RoboLogger;
use Symfony\Component\Console\Output\OutputInterface;

class Logger extends RoboLogger implements SuccessInterface
class Logger extends RoboLogger
{
public function __construct(OutputInterface $output)
{
Expand Down Expand Up @@ -73,9 +72,4 @@ public static function formatSize($size)
return str_replace('@size', round($size, 2), $unit);
}
}

public function success($message, array $context = array())
{
$this->log(ConsoleLogLevel::SUCCESS, $message, $context);
}
}
13 changes: 7 additions & 6 deletions src/Runtime/DependencyInjection.php
Expand Up @@ -2,6 +2,7 @@

namespace Drush\Runtime;

use Drush\Log\Logger;
use League\Container\Container;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function initContainer(
$container = new Container();

// With league/container 3.x, first call wins, so add Drush services first.
$this->addDrushServices($container, $loader, $drupalFinder, $aliasManager, $config);
$this->addDrushServices($container, $loader, $drupalFinder, $aliasManager, $config, $output);

// Robo has the same signature for configureContainer in 1.x, 2.x and 3.x.
Robo::configureContainer($container, $application, $config, $input, $output);
Expand Down Expand Up @@ -93,12 +94,12 @@ public function installHandlers($container): void
}

// Add Drush Services to league/container 3.x
protected function addDrushServices($container, ClassLoader $loader, DrupalFinder $drupalFinder, SiteAliasManager $aliasManager, DrushConfig $config): void
protected function addDrushServices($container, ClassLoader $loader, DrupalFinder $drupalFinder, SiteAliasManager $aliasManager, DrushConfig $config, OutputInterface $output): void
{
// Override Robo's logger with our own
$container->share('logger', 'Drush\Log\Logger')
->addArgument('output')
->addMethodCall('setLogOutputStyler', ['logStyler']);
// Override Robo's logger with a LoggerManager that delegates to the Drush logger.
$container->share('logger', '\Drush\Log\DrushLoggerManager')
->addMethodCall('setLogOutputStyler', ['logStyler'])
->addMethodCall('add', ['drush', new Logger($output)]);

$container->share('loader', $loader);
$container->share('site.alias.manager', $aliasManager);
Expand Down
25 changes: 24 additions & 1 deletion sut/drush/Commands/SimpleSutCommands.php
Expand Up @@ -5,8 +5,12 @@
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface;
use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait;
use Consolidation\Log\ConsoleLogLevel;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drush\Drush;
use Drush\Symfony\DrushArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Drush\Commands\DrushCommands;
Expand All @@ -21,7 +25,7 @@
class SimpleSutCommands extends DrushCommands
{
/**
* Show a fabulous picture.
* Show a message.
*
* @command sut:simple
* @hidden
Expand All @@ -30,4 +34,23 @@ public function example()
{
$this->logger()->notice(dt("This is an example site-wide command committed to the repository in the SUT inside of the 'drush/Commands' directory."));
}

/**
* Replace Drush logger with a custom one.
*
* In a real-world implementation, you would likely use `@hook *` instead of `@hook sut:simple`.
*
* @hook init sut:simple
*/
public function customLogger(DrushArgvInput $argv, AnnotationData $annotationData): void
{
$verbosityLevelMap = [ConsoleLogLevel::SUCCESS => OutputInterface::VERBOSITY_NORMAL];
$formatLevelMap = [ConsoleLogLevel::SUCCESS => \Psr\Log\LogLevel::INFO];
// One could use Monolog if desired.
// Drush expects custom loggers to always write to stderr, so dont use ConsoleLogger directly,
$newLogger = new ConsoleLogger(Drush::output(), $verbosityLevelMap, $formatLevelMap);
$drushLoggerManager = $this->logger();
$drushLoggerManager->reset()->add('foo', $newLogger);
}
}

17 changes: 17 additions & 0 deletions tests/integration/CustomLoggerTest.php
@@ -0,0 +1,17 @@
<?php

namespace Unish;

class CustomLoggerTest extends UnishIntegrationTestCase
{
public function testCustomLogger()
{
// Uses standard Drush logger.
$this->drush('version', [], ['debug' => true]);
$this->assertStringContainsString('sec', $this->getErrorOutputRaw());

// sut:simple has been hooked so that a custom logger is use. It doesn't show timing information during --debug.
$this->drush('sut:simple', [], ['debug' => true, 'simulate' => true]);
$this->assertStringNotContainsString('sec', $this->getOutput());
}
}