Problem/Motivation

The Extended Logger allows storing not only the plain key-value scalar values, but also structured logs with nested structures.

But Drupal placeholders support only plain keys and plain values; even dots are not allowed there!

Fortunately, Drupal's LogMessageParser also supports PSR3 style messages, which use curly brackets and allow using dots in the placeholder text!

But, unfortunately, this will not work out of the box, because Drupal's LogMessageParser doesn't support nested values.

Proposed resolution

The good news is that we can extend Drupal's default LogMessageParser by adding support for dot-represented paths using the NestedArray utility.

And we can even extend this by supporting JSONPath expressions for more complex queries - this will not follow the PSR3 style placeholders already, because symbols like $ are not allowed there, but still we will have the backwards compatibility.

Also, would be good to provide an option for backward compatibility for the default Drupal logger and other logger types, by copying the nested values as keys of the plain $context array - this should make them work well with the default Drupal logger.

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

murz created an issue. See original summary.

murz’s picture

Issue summary: View changes

  • murz committed 2aeb4565 on 1.3.x
    Issue #3537997: Allow JSONPath in the Message placeholders
    
murz’s picture

Status: Active » Fixed

Implemented, now we can use messages with tokens like this:

  \Drupal::logger('my_module')->warning('My message: {metadata.values.val1} and {$.metadata.values[\'val2\']} and that\'s it', [
      'metadata' => [
        'values' => [
          'val1' => "My value 1",
          'val2' => "My value 2",
        ],
      ],
    ]);

And the final message in the log entry will be:
My message: My value 1 and My value 2 and that's it

Also, introduced a submodule "extended_logger_fallback" that replaces the Drupal Core's service logger.log_message_parser to a custom one, that extends the base service with JSONPath placeholders. With it, this will work even for Drupal Core loggers like dblog and syslog!

murz’s picture

Status: Fixed » Closed (fixed)