Since D8 introduced PSR-3 logging, our logging system supports both legacy-style placeholders prefixed by "@", "!", or "%", and the standard single-brace wrapped placeholders. PSR-3 placeholders are converted to legacy style in LogMessageParser::parseMessagePlaceholders().

However, the conversion being performed is not entirely correct, as it prevents messages containing braces beyond the placeholders from being kept, because the transformation is too eager. Case in point: logging information about a route error like:

$logger->error('Incorrect parameter {{foo}} in path {path}: {value}', ['foo' => 'bar', 'path' => '/baz', 'value' => 'quux']);
  • Expected result: "Incorrect parameter {bar} in path /baz: quux"
  • Actual result: "Incorrect parameter @{foo} in path /baz: quux

Assigned to dblog since the core component list does not include the logger component.

Comments

fgm created an issue. See original summary.

fgm’s picture

Status: Active » Needs review
StatusFileSize
new1.35 KB

Patch seems to work on manual tests. Checking with the whole test suite.

Status: Needs review » Needs work

The last submitted patch, 2: 2909805-logger-braces.patch, failed testing. View results

fgm’s picture

Status: Needs work » Needs review

Seems to be a random test bot fluke: the fail does not appear to have anything to do with the patch.

dagmar’s picture

Status: Needs review » Needs work
+++ b/composer.json
@@ -5,7 +5,8 @@
-        "wikimedia/composer-merge-plugin": "^1.4"
+        "wikimedia/composer-merge-plugin": "^1.4",
+        "php-amqplib/php-amqplib": "^2.6"
     },
     "replace": {
         "drupal/core": "^8.4"
@@ -60,5 +61,9 @@

@@ -60,5 +61,9 @@
             "type": "composer",
             "url": "https://packages.drupal.org/8"
         }
-    ]
+    ],
+    "require-dev": {
+        "drush/drush": "^9",
+        "drupal/devel": "1.x-dev"
+    }

It seems this changes are unintended. And probably the cause of the test failure.

fgm’s picture

Status: Needs work » Needs review
StatusFileSize
new726 bytes

Oh, you're right, of course, thanks. Rerolled.

googletorp’s picture

Status: Needs review » Needs work
StatusFileSize
new1.12 KB
new1.83 KB

Added test case to patch from #6 and a test only patch.

googletorp’s picture

Status: Needs work » Needs review
fgm’s picture

Nice to have a test for dblog. However, the issue is more generic, since it is in the logger component, not in dblog itself. It would be even nicer to add a more generic test using a test logger backend so that this handling would be more generic.

The last submitted patch, 7: 2909805-7-test-only.patch, failed testing. View results

dagmar’s picture

It would be even nicer to add a more generic test using a test logger backend so that this handling would be more generic.

There is an intent of make a generic logger here: #2862282: Add a test logger channel for test base classes to use. In order to have a unified interface with at least dblog, I proposed to finish first #2458191: Provide a storage backend for dblog module

+++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
@@ -119,6 +119,23 @@ public function testLogEventPage() {
+   * Test individual log event page.

This docs seems duplicated, and does not illustrate the idea behind this test.

fgm’s picture

FWIW, the MongoDB Logger/Watchdog, at least in D7 includes actual unit tests mocking the actual connection. We could have the same here, I suspect.

googletorp’s picture

StatusFileSize
new1.5 KB
new2.11 KB
new2.82 KB

So I

- Added test for LogMessageParser service.
- Fixed the copy/paste comment in the new DBLog test

Uploadet, patch, test only patch and interdiff.

You could argue that DbLog test is not strictly needed, since we have a test in the parser class. Since we did write the code and the DbLog was were the bug surfaced, I think that it makes sense to keep the test to verify that the change fixed the issue.

Status: Needs review » Needs work

The last submitted patch, 13: 2909805-13.patch, failed testing. View results

googletorp’s picture

Status: Needs work » Needs review

Updating status back to NR - seems like test failed due to some database issues on test machine.

googletorp’s picture

googletorp’s picture

dagmar’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Logger/LogMessageParser.php
    @@ -17,7 +17,7 @@ public function parseMessagePlaceholders(&$message, array &$context) {
           // Transform PSR3 style messages containing placeholders to
    

    This comment now also includes twigs placeholders.

  2. +++ b/core/modules/dblog/tests/src/Functional/DbLogTest.php
    @@ -119,6 +119,23 @@ public function testLogEventPage() {
    +    $this->drupalLogin($this->adminUser);
    

    If we are going to test this. I would like to see also all the other placeholders like @placeholder, %placeholder and :placeholder

    Those cases are covered here #2760031: Log messages won't appear translated if they have special characters in them. My proposal then is remove the method testMessageParsing from this patch and move the twig case to #2760031 after this patch be committed.

  3. +++ b/core/tests/Drupal/Tests/Core/Logger/LogMessageParserTest.php
    @@ -63,6 +63,11 @@ public function providerTestParseMessagePlaceholders() {
    +      // Message with PSR3 and twig style messagea.
    

    There is a typo at the end of this string.

googletorp’s picture

StatusFileSize
new2.81 KB
new2.1 KB
new1.29 KB

18.1 => {{foo}} and {foo} are both PSR3 placeholders it's basically PLACEHOLDER and {PLACEHOLDER}, so no need to update.
18.2 => What we are testing is convention of {{foo}} => {@foo} - the output of other kind out placeholders are outside of the scope for this issue
18.3 => I corrected typo by removing the new test and simply changing the double PSR3 tests to have single and double {} variants.

New files attached

googletorp’s picture

Status: Needs work » Needs review

Ops forgot to change status

Status: Needs review » Needs work

The last submitted patch, 19: 2909805-19-test-only.patch, failed testing. View results

dagmar’s picture

Status: Needs work » Needs review
StatusFileSize
new10.21 KB

So, I've been playing with this patch and seems is working fine. However, when I try this new patch together with #2760031: Log messages won't appear translated if they have special characters in them, I see the following message:

Deprecated: Invalid placeholder (path) in string: Incorrect parameter {bar} in path /baz: horse in /var/www/drupal/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 238

Deprecated: Invalid placeholder (path) in string: Incorrect parameter {bar} in path /baz: horse in /var/www/drupal/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 238

Which makes sense since {{ value }} cannot be translated See: https://www.drupal.org/node/2047135

Therefore, if we commit this feature, as is (which works) then #2760031: Log messages won't appear translated if they have special characters in them will be blocked forever.

I'm attaching the patch with the 2 patches in the same file.

Status: Needs review » Needs work

The last submitted patch, 22: 2909805-22.patch, failed testing. View results

googletorp’s picture

Status: Needs work » Needs review

#22 Merging two issues is probably a bad idea. This issue doesn't touch tranlations of variables. Today {foo} works but {{foor}} doesn't - since {{foo}} is converted to @{foo} instead of {@foo}.

If this issue gives new issues to #2760031, we should handle it there and try to merge these two issues as they aren't related to each other.

I'm removing the patch uploaded in #22 so we can continue with the proposed fix #19.

cburschka’s picture

The patch in #19 looks good. Simple regex fix, and the tests seem to cover it correctly.

Note: As per https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logg...,

Placeholder names SHOULD be composed only of the characters A-Z, a-z, 0-9, underscore _, and period .. The use of other characters is reserved for future modifications of the placeholders specification.

Should we make this "SHOULD" mandatory and use [A-Za-z0-9_.] instead of [^\{}]? Other than that I believe this is ready.

dagmar’s picture

Merging two issues is probably a bad idea.

I know, my intention just was to point out that the this patch has side effects.

If this issue gives new issues to #2760031, we should handle it there and try to merge these two issues as they aren't related to each other.

I'm tagging this with Needs framework manager review to decide if we want to decide that logs cannot be translated anymore. I might be wrong, but as I mentioned in #22, showing {{ twig }} will make impossible to translate logs.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

mxr576’s picture

Version: 8.8.x-dev » 9.1.x-dev

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dagmar’s picture

Status: Needs review » Needs work
Issue tags: -Needs framework manager review +Novice, +Needs reroll
StatusFileSize
new11.41 KB
new11.41 KB
new13.38 KB

Long time passed since I reviewed this issue. My concerns about translations were not valid. I manually applied the patch (does not apply anymore) and managed to translate the log without issues.

Translation example

Translation example

This seems good to go in my opinion. But the patch does not apply anymore to 9.3.x

suresh prabhu parkala’s picture

Status: Needs work » Needs review
StatusFileSize
new10.41 KB

Re-rolled patch against 9.3.x. Please review.

dagmar’s picture

Status: Needs review » Needs work

The patch that needs a reroll is this one. #19

ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new2.85 KB
new2.2 KB

Re-rolled the patch in #19 as requested in #35, thanks!

dagmar’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @ankithashetty

I think this looks good. There is a question about if the current pattern should be modified, but since this fix the original issue I think this qualifies as a follow up with more tests. #25

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed f653102333 to 9.3.x and 0865322d5b to 9.2.x. Thanks!

+++ b/core/lib/Drupal/Core/Logger/LogMessageParser.php
@@ -17,7 +17,7 @@ public function parseMessagePlaceholders(&$message, array &$context) {
-      $message = preg_replace('/\{(.*)\}/U', '@$1', $message);
+      $message = preg_replace('/\{([^\{}]*)\}/U', '@$1', $message);

The escaping here is a a little inconsistent but it doesn't matter. I think curly braces only have meaning if there is a number after them so quite possibly all the escaping is unnecessary which would helpful with readability.

  • alexpott committed f653102 on 9.3.x
    Issue #2909805 by googletorp, fgm, dagmar, ankithashetty:...

  • alexpott committed 0865322 on 9.2.x
    Issue #2909805 by googletorp, fgm, dagmar, ankithashetty:...
googletorp’s picture

Wow, 4 years later and it's in, I forgot I ever worked on this, thanks @dagmar for doing the work to get this over the finish line :)

Status: Fixed » Closed (fixed)

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