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

in Drush version 12, the drushLog doesn't get registered as a logger with drupal's logger.factory. #5762

Closed
ebenshap opened this issue Sep 19, 2023 · 2 comments

Comments

@ebenshap
Copy link

ebenshap commented Sep 19, 2023

Describe the bug
A clear and concise description of what the bug is.

When running a command from the terminal that uses Drupal's logger.factory service to print to the screen, nothing prints to the screen.

To Reproduce
What did you do?

Update your site to run Drush 12. Create a module with a command class, and add this method to it:

/**
  * Test.
  *
  * @command test_module:test
  */
 public function testLogger() {

   $logFactory = \Drupal::service('logger.factory');
   $logger = $logFactory->get('asdf');
   $logger->notice('hello');

 }

Expected behavior
What did you expect would happen?

After running this command from the terminal:
drush test_module:test

This should be printed to the screen:
[notice] hello

Actual behavior
What happened instead?

Nothing gets printed to the screen.

Workaround
Is there another way to do the desired action?

Add this to the web/sites/default/settings.php file:

// Coax \Drupal\Core\DrupalKernel::discoverServiceProviders to add our logger.
use Drush\Drupal\DrushLoggerServiceProvider;

$GLOBALS['conf']['container_service_providers'][] = DrushLoggerServiceProvider::class;

clear the cache, and now the drush test_module:test command should print to the screen.

System Configuration

Q A
Drush version? 12.x
Drupal version? 10.x
PHP version 8.x
OS? Mac

Additional information
Add any other context about the problem here.

@weitzman
Copy link
Member

Most commands use the Drush logger via $this->logger(). If you want to log to Drupal and Drush, use Drupal's messenger service.

In general, I can see that Drush's Drupal->Drush logging is working. Note the [info] ban module installed. message below. Thats from \Drupal\Core\Extension\ModuleInstaller::install

weitzman@drush-web:/var/www/html$ drush pm:install ban -vvv
 [preflight] Config paths: /var/www/html/drush.yml,/var/www/html/drush
 [preflight] Alias paths: /var/www/html/sut/drush/sites,/var/www/html/drush/sites
 [preflight] Commandfile search paths: /var/www/html/src,/var/www/html/sut/drush,/var/www/html/sut/sites/all/drush
 [debug] Bootstrap further to find pm:install [1.21 sec, 2.87 MB]
 [debug] Trying to bootstrap as far as we can [1.21 sec, 2.87 MB]
 [info] Drush bootstrap phase: bootstrapDrupalRoot() [1.21 sec, 2.88 MB]
 [info] Change working directory to /var/www/html/sut [1.21 sec, 2.88 MB]
 [info] Initialized Drupal 10.1.4 root directory at /var/www/html/sut [1.21 sec, 2.88 MB]
 [info] Drush bootstrap phase: bootstrapDrupalSite() [1.22 sec, 2.95 MB]
 [debug] Could not find a Drush config file at sites/dev/drush.yml. [1.22 sec, 3.01 MB]
 [info] Initialized Drupal site drush.ddev.site:444 at sites/dev [1.22 sec, 3.01 MB]
 [info] Drush bootstrap phase: bootstrapDrupalConfiguration() [1.22 sec, 3.01 MB]
 [info] Drush bootstrap phase: bootstrapDrupalDatabase() [1.23 sec, 3.2 MB]
 [info] Successfully connected to the Drupal database. [1.23 sec, 3.2 MB]
 [info] Drush bootstrap phase: bootstrapDrupalFull() [1.23 sec, 3.2 MB]
 [debug] Start bootstrap of the Drupal Kernel. [1.23 sec, 3.2 MB]
 [debug] Finished bootstrap of the Drupal Kernel. [1.25 sec, 3.83 MB]
 [debug] Loading drupal module drush commands & etc. [1.25 sec, 3.83 MB]
 [debug] Add a commandfile class: Drush\Drupal\Commands\sql\SanitizeCommands [1.27 sec, 4.04 MB]
 [debug] Add a commandfile class: Drush\Drupal\Commands\sql\SanitizeCommentsCommands [1.27 sec, 4.05 MB]
 [debug] Add a commandfile class: Drush\Drupal\Commands\sql\SanitizeSessionsCommands [1.27 sec, 4.05 MB]
 [debug] Add a commandfile class: Drush\Drupal\Commands\sql\SanitizeUserFieldsCommands [1.28 sec, 4.05 MB]
 [debug] Add a commandfile class: Drush\Drupal\Commands\sql\SanitizeUserTableCommands [1.28 sec, 4.06 MB]
 [debug] Done with bootstrap max in Application::bootstrapAndFind(): trying to find pm:install again. [1.48 sec, 6.38 MB]
 [info] Starting bootstrap to none [1.5 sec, 6.46 MB]
 [info] Drush bootstrap phase 0 [1.5 sec, 6.46 MB]
 [info] Try to validate bootstrap phase 0 [1.5 sec, 6.46 MB]
 [debug] Get container builder [1.7 sec, 8.61 MB]
 [info] ban module installed. [3.46 sec, 14.56 MB]
 [success] Successfully enabled: ban [3.73 sec, 15.19 MB]

@apotek
Copy link
Contributor

apotek commented Apr 18, 2024

Using Drush 12, on Drupal 10.2 I can see that neither \Drupal::messenger()->addError() nor $this->logger()->error() (from within a Drush command) will print to Drupal's log. These services will only print to the screen. (Messenger, called in a drush command does not write to dblog, nor does $this->logger()->error()). So they are not replacements for having been able to use logger factory, which (in Drush 11) allowed one to write to drupal log and to the terminal.

This means that, to work with Drush 12 all my modules need to be rewritten to write the same message twice: Once, using logger factory to write to dblog, and once, using either messenger or drush logger to print to the screen. Gold star 🌟!

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

No branches or pull requests

3 participants