1

I'm trying to log a stacktrace when I throw an exception as suggested here.

I tried this:

  catch (\Exception $e) {
    \Drupal::logger('mymodule')->error($e->getMessage(), [ 'exception' => $e]);
  }

However, the stacktrace doesn't get logged; I only see the error message. How can I log the stacktrace as well?

2 Answers 2

1

This is not possible in Drupal 9 because the Drupal 9 logger is not PSR-3 compatible.

However, Drupal 10 alpha already supports PSR-3 logging, so this feature will be available shortly.

1

This works fine for Drupal 8 and 9:

catch (\Exception $e) {
  \Drupal::logger('mymodule')->error("<pre>$e</pre>");
}

This works because Exception::__toString includes the stack trace. <pre> ensures that line breaks are shown properly in your browser when looking at the error logs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.