phpdrupaldrupal-8drupal-themingdrupal-9

How to use Drupal 8 logger within the .theme file


I need to debug a preprocess function in a theme's .theme file. I don't have the project on my local computer, so I can't use dump-like functions on production.

I thought about using logs, but \Drupal::logger() requires a module name as its first parameter, and I'm not within a module, I'm at the .theme file.

How can I use this or a similar function?


Solution

  • Drupal::logger() does not require a module name as its first parameter, the general practice is to use the name of the module calling it, but you can use any string you want actually.

    This string is used as a channel (or category or type) to which the message belongs, it principally aim easing the idendification of messages when looking through logs (ie. by filtering them by their category/type/channel).

    Here on the Reports page of the admin UI, it is referred to as Type :

    Admin UI Logs screenshot