LogEntry causes erroneous records in "locales_source" table

Created on 5 July 2021, over 3 years ago
Updated 24 November 2024, 10 days ago

Problem/Motivation

Our setup has heavy logging enabled via \Drupal::logger($channel)->debug($message) and this appears to be wrapped with Ultimate Cron's LogEntry.

On line 115, the class takes a variable and passes it through a t() string which is bad practise:

  public function log($message, $variables = array(), $level = RfcLogLevel::NOTICE) {

    if ($variables !== NULL && gettype($message) === 'string') {
      $message = t($message, $variables);
    }

A warning is also emitted:

phpcs: Only string literals should be passed to t() where possible

.

This also causes our locales_source table flooded with erroneous string translations with tons of variations like the below:

@"response":{"statusCode":200,"headers":{"Cache-Control":{"0":"no-cache, no-store, private, must-revalidate","Content-Type":@"0":"application\/json;charset=utf-8","Date":@"0":"Mon, 05 Jul 2021 14:33:42 GMT","Server":@"0":"Apache-Coyote\/1.1","set-cookie":@"0":"_csrf=0NQxvg51h51-lCghvX3gcMho; Path=\/","Vary":@"0":"Accept-Encoding","X-Application-Context":@"0":"api-gateway:aws,redis:8443","X-Content-Type-Options":@"0":"nosniff","X-Frame-Options":@"0":"DENY","1":"DENY","X-SL-RequestId":@"0":"babdf359-7f2f-475a-80fa-70c40v2a489f","X-XSS-Protection":@"0":"1; mode=block","1":"1; mode=block","Content-Length":@"0":"51","Connection":@"0":"keep-alive"},"body":"@\"response\":{\"code\":\"SUCCESS\",\"data\":{\"items\":[]}}"}}

Steps to reproduce

  • Install Ultimate Cron latest 2.x version
  • Add loggin via Drupal::logger() to an implementation via hook_cron()
  • Run the cron task via Ultimate Cron
  • Check in Translation Interface for Locale strings ran through t()

Proposed resolution

It looks like the if clause is needlessly ran through when $variables is being checked as NULL where in all cases through my testing has been an empty array.

Changing this to !empty($variables) gets rid of the problem in my case, and aligned with the executing code block at /src/Logger/WatchdogLogger.php ahead of the logging at LogMessageParserInterface::parseMessagePlaceholders() but still suggests maybe for a better alternative on the usage of t() here.

Remaining tasks

Agree on a solution, write a patch / MR and review.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇧🇪Belgium baikho Antwerp, BE

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024