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

Allow command classes to set their own logger channel #5963

Merged
merged 10 commits into from
May 2, 2024

Conversation

jurgenhaas
Copy link
Contributor

@jurgenhaas jurgenhaas commented Apr 24, 2024

Following this thread on Drupal Slack the ServiceManager inflection overrides the logger channel of a Drush command, if that came with its own channel to e.g. log to the Drupal channel e.g. in watchdog.

With this PR, the service manager would only set its logger if the command had not defined their own before.

@jonathan1055
Copy link
Contributor

Is there a way that I could test this change in a phpunit test run in gitlab_templates pipeline? Happy to help investigate, but not sure how to do it.

@jurgenhaas
Copy link
Contributor Author

@jonathan1055 not sure TBH. I haven't ever php unit tested a module in a drush context. Not sure if that's a thing.

@jonathan1055
Copy link
Contributor

The contrib module provides drush commands, and I have a phpunit test that checks the commands work as expected. I have had to disable that test group for a while. It is still working correctly on DrupalCI phpunit test, but not in gitlab pipelines.

*
* @param LoggerInterface $logger
*/
public function setLoggerIfEmpty(LoggerInterface $logger): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used by anything other than the service manager? If not, I think it would be better to just move the condition to inflect, and omit the extra trivial function in the base class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do that, the logger variable is protected and can't be accessed from the service manager.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about making logger() public and using it here? maybe only on 13.x branch due to BC.

if (!$object->logger()) {
  $object->setLogger($container->get('logger'))
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can tell whether the logger variable is set via

protected function logger(): ?DrushLoggerManager

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That method is protected, so we can't call it from outside, i.e. from the service manager.

@@ -452,7 +452,7 @@ public function inflect(DrushContainer $container, $object): void
$object->setConfig($container->get('config'));
}
if ($object instanceof LoggerAwareInterface) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I guess we can't check $object->logger() here as I suggested unless we make logger() public. Did not consider impact of that, I think that method is defined in another project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That method is in Drush so we can change it but only for Drush 13 due to BC. I'll open a new PR for that, and fixup this one for Drush 12.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went the other way around. The Drush 12.x PR is at #5980. This PR will be for 13.x

@@ -452,7 +452,7 @@ public function inflect(DrushContainer $container, $object): void
$object->setConfig($container->get('config'));
}
if ($object instanceof LoggerAwareInterface) {
$object->setLogger($container->get('logger'));
$object->setLoggerIfEmpty($container->get('logger'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setLoggerIfEmpty() is not a method of LoggerAwareInterface, so there is a mismatch between the instanceof above and the usage here.

@weitzman weitzman changed the base branch from 13.x to 12.x May 2, 2024 15:24
@weitzman weitzman changed the base branch from 12.x to 13.x May 2, 2024 15:25
@weitzman
Copy link
Member

weitzman commented May 2, 2024

Now this is a small, helpful change for 13.x. Thanks everyone.

@weitzman weitzman merged commit f98235d into drush-ops:13.x May 2, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants