Problem/Motivation

Sometimes it would be helpful to be able to log to watchdog from twig, for example if you encounter an unexpected misconfiguration you need to log, like an expected, but missing field or key.

https://drupalize.me/blog/201510/how-log-messages-drupal-8

So it would be nice to have a twig function for this like:
{{ drupal_logger('my_module', 'notice', 'Missing expected field XYZ' }}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork twig_tweak-3309384

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anybody created an issue. See original summary.

anybody’s picture

This is to be discussed. What do you think about this @Chi? I didn't find any existing solution yet.

chi’s picture

Status: Active » Closed (won't fix)

The use case is too specific and a workaround is simple.

/**
 * Implements hook_twig_tweak_functions_alter().
 */
function EXAMPLE_twig_tweak_functions_alter(array &$functions): void {
  $drupal_log = static function(
    string $module,
    string $severity,
    string|\Stringable $message,
    array $context = [],
  ): void {
    \Drupal::logger($module)->{$severity}($message, $context);
  };
  $functions[] = new TwigFunction('drupal_log', $drupal_log);
}
anybody’s picture

Thanks @Chi for the snippet to create the simple twig custom function! We'll discuss, how we can solve this.
As I found bamboo_twig as wider alternative some days ago, I've created the same request there to see if it's relevant: #3311609: Add a logger call for twig
Perhaps it helps others, but I totally understand you want to keep twig_tweak focused!

Thanks a lot again :)

thomas.frobieter’s picture

I still think this is a very useful feature!

Especially if the template contains logic that could fail and should not fail silently.

anybody’s picture

Version: 3.x-dev » 4.x-dev
Status: Closed (won't fix) » Active

Reopening this as IMHO it's useful as @thomas.frobieter wrote.

We'll prepare a MR.

anybody’s picture

Assigned: Unassigned » grevil
Status: Active » Needs work

grevil made their first commit to this issue’s fork.

grevil’s picture

Assigned: grevil » Unassigned
Status: Needs work » Needs review

Done, please review!

grevil’s picture

Assigned: Unassigned » grevil
Status: Needs review » Needs work
grevil’s picture

Assigned: grevil » Unassigned
Status: Needs work » Needs review

Test failures are unrelated. The adjusted test method works as expected! Please review!

anybody’s picture

Nice @grevil!!

Some ideas for @thomas.frobieter's feedback:

  1. Should we have a shorthand? Maybe dl()? Is there a known shorthand in devel perhaps?
  2. Message is typically the "most important", but is now the last parameter, like in PHP, which makes sense... For twig I'd really love {{ drupal_logger('Missing expected field XYZ') }} with a channel fallback to "twig_tweak:drupal_logger" and level to "notice", but I'm not totally sure we should turn the parameters around
  3. Please document the third parameter as a second example
thomas.frobieter’s picture

Assigned: Unassigned » thomas.frobieter
thomas.frobieter’s picture

Status: Needs review » Reviewed & tested by the community

LGTM

anybody’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, merged!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

anybody’s picture

Assigned: thomas.frobieter » Unassigned

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.