Closed (fixed)
Project:
Twig Tweak
Version:
4.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
13 Sep 2022 at 09:29 UTC
Updated:
5 May 2026 at 10:20 UTC
Jump to comment: Most recent
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' }}
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
Comment #2
anybodyThis is to be discussed. What do you think about this @Chi? I didn't find any existing solution yet.
Comment #3
chi commentedThe use case is too specific and a workaround is simple.
Comment #4
anybodyThanks @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 :)
Comment #5
thomas.frobieterI still think this is a very useful feature!
Especially if the template contains logic that could fail and should not fail silently.
Comment #6
anybodyReopening this as IMHO it's useful as @thomas.frobieter wrote.
We'll prepare a MR.
Comment #7
anybodyComment #9
grevil commentedDone, please review!
Comment #11
grevil commentedComment #12
grevil commentedTest failures are unrelated. The adjusted test method works as expected! Please review!
Comment #13
anybodyNice @grevil!!
Some ideas for @thomas.frobieter's feedback:
dl()? Is there a known shorthand in devel perhaps?{{ 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 aroundComment #14
thomas.frobieterComment #15
thomas.frobieterLGTM
Comment #16
anybodyThanks, merged!
Comment #19
anybody