Skip to content

Commit

Permalink
Allow command classes to set their own logger channel (12.x) (#5980)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed May 2, 2024
1 parent 7ae3de8 commit 4aebed8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Commands/DrushCommands.php
Expand Up @@ -17,8 +17,10 @@
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use JetBrains\PhpStorm\Deprecated;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Robo\Common\IO;
use Robo\Contract\ConfigAwareInterface;
use Robo\Contract\IOAwareInterface;
Expand Down Expand Up @@ -64,6 +66,17 @@ protected function io(): SymfonyStyle
return $this->io;
}

/**
* Sets a logger, if none is available yet.
*/
#[Deprecated('Use logger() in Drush 13+')]
public function setLoggerIfEmpty(LoggerInterface $logger): void
{
if ($this->logger === null) {
$this->setLogger($logger);
}
}

/**
* Returns a logger object.
*/
Expand Down
5 changes: 2 additions & 3 deletions src/Runtime/ServiceManager.php
Expand Up @@ -23,7 +23,6 @@
use Grasmash\YamlCli\Command\UpdateKeyCommand;
use Grasmash\YamlCli\Command\UpdateValueCommand;
use Psr\Container\ContainerInterface as DrushContainer;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Robo\ClassDiscovery\RelativeNamespaceDiscovery;
use Robo\Contract\ConfigAwareInterface;
Expand Down Expand Up @@ -462,8 +461,8 @@ public function inflect(DrushContainer $container, $object): void
if ($object instanceof ConfigAwareInterface) {
$object->setConfig($container->get('config'));
}
if ($object instanceof LoggerAwareInterface) {
$object->setLogger($container->get('logger'));
if (method_exists($object, 'setLoggerIfEmpty')) {
$object->setLoggerIfEmpty($container->get('logger'));
}
// Made available by DrushCommands (must preserve for basic bc)
if ($object instanceof ProcessManagerAwareInterface) {
Expand Down

0 comments on commit 4aebed8

Please sign in to comment.