Problem/Motivation

If you have e.g. an exception when writing to a user table, then \Drupal\Core\Logger\LogMessageParser::parseMessagePlaceholders() converts {users_field_data} to @users_field_data and then, because we have a context key 'user', it tries to put the AccountProxy object in there, which gives you this recoverable fatal error:

Object of class Drupal\Core\Session\AccountProxy could not be converted to string

Proposed resolution

Only replace {something} with @something if we actually have something in $context?

Possibly also explicitly exclude keys that we do not want to get replaces, so even if you have {user}, that it will not try to replace it.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Berdir created an issue. See original summary.

edurenye’s picture

Assigned: Unassigned » edurenye

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dagmar’s picture

Component: watchdog.module » dblog.module

Moved to dblog.module for future triage.

berdir’s picture

Assigned: edurenye » Unassigned
dagmar’s picture

@Berdir This seems legit, can you provide a way to replicate this issue so we can write a test to expose it? Thanks

berdir’s picture

Just run this:

\Drupal::logger('foo')->warning('This is enough to reproduce it: {users_field_data}');

Then access /admin/reports/dblog. That "only" gives you a warning, but e.g. drush ps dies with an exception.

dagmar’s picture

Priority: Major » Normal

Thanks @Berdir. So, the only way to get this fatal error if is a log message includes the {users_field_data} in the message and not as a placeholder replacement (Which is what drupal usually do). I tried to trigger an exception as the IS says:

    $me = User::load(1);
    $me->setUsername('');
    $me->save();

And this allows me to use drush ws. Still a valid issue. I'm just changing the priority.

berdir’s picture

Your example works because the error message itself is an argument, so it's not getting further replaced. Which is the default with watchdog_exception()

Fine with changing to normal, in our case we had it in combination with migrate exceptions that where logged with custom error handling I think.

michfuer’s picture

I'm seeing the same error when trying to log a message like

$this->logger->warning('Some sweet message about user {user_id} with fields {fields}', $context);

@user is getting added to the context. Looks like line 28 of LogMessageParser.php (on Drupal 8.2.7)

if (strpos($message, '@' . $key) !== FALSE) {
  $key = '@' . $key;
}

what about trying an exact match, something like

$pattern = '/\b@' . $key . '\b/';
if (preg_match($pattern, $message)) {
  $key = '@' . $key;
}

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.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.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.

dagmar’s picture

Status: Active » Closed (duplicate)

Marking as duplicate of #2986294: Remove $context['user'] from LoggerChannel::log @Berdir could you take a look to the patch provided there?