Problem/Motivation

The return value of Error::decodeException contains a key called backtrace. This is often very large and contains a considerable number of large complex entities and objects. There are instances in core (and contrib #2954120: Return values of Error::decodeException not compatible with some loggers.) where this information is passed directly to \Psr\Log\LoggerInterface::log as part of $context.

Context does sound quite vague, however I do believe it's probably incorrect to pass megabytes of data into it, when it's reasonable to expect logging implementations will try and store this info. When used in conjunction with monolog for example, log messages are never stored due to the size of the callstack ($context is normalized during logging). From the PSR:

Every method accepts an array as context data. This is meant to hold any extraneous information that does not fit well in a string. The array can contain anything. Implementors MUST ensure they treat context data with as much lenience as possible. A given value in the context MUST NOT throw an exception nor raise any php error, warning or notice.

The reason this isn't an issue for core is, the first thing we do is unset 'backtrace' in DbLog:

    public function log($level, $message, array $context = []) {
    // Remove any backtraces since they may contain an unserializable variable.
    unset($context['backtrace']);

Proposed resolution

We should make an effort to squash all instances of 'backtrace' before they are passed to the logger interface. In the meantime, individual loggers can take steps to remove the key, however I believe the root cause of the issue should be addressed.

Remaining tasks

Agree and patch.

User interface changes

API changes

Data model changes

Comments

Sam152 created an issue. See original summary.

sam152’s picture

Issue summary: View changes
sam152’s picture

Issue summary: View changes
sam152’s picture

Title: The return value of Error::decodeException in it's current form should never be passed to LoggerInterface::log. » The return value of Error::decodeException should never be passed to LoggerInterface::log.
sam152’s picture

sam152’s picture

Issue summary: View changes
sam152’s picture

Issue summary: View changes
sam152’s picture

Issue summary: View changes
sam152’s picture

Issue summary: View changes
jibran’s picture

mfb’s picture

I've been trying to go in the other direction - providing more context to logger implementations as they can log it (often times with a nice user interface) and use it to fingerprint issues (linking exceptions together as the same issue). e.g. we added the raw backtrace for PHP errors at #2857437: pass raw backtrace to loggers for errors, not just exceptions since previously it was only being passed for exceptions.

I haven't looked around for what logger implementations are out there, but the one I maintain https://www.drupal.org/project/raven would regenerate the stacktrace if you don't provide one. Since core exception handler has it, why not pass it to a custom logger implementation to use if it can?

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.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.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.

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.

mfb’s picture

I think this issue could be closed - we're now passing the exception object to loggers as well in #2949419: Pass the raw exception to logger implementations

As the PSR standard states

The array can contain anything. Implementors MUST ensure they treat context data with as much lenience as possible. A given value in the context MUST NOT throw an exception nor raise any php error, warning or notice.

it's basically up to loggers to figure out how to represent or ignore each element of context data.

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.

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mfb’s picture

Status: Active » Closed (works as designed)

Closing due to lack of discussion, but feel free to re-open