Skip to content
Snippets Groups Projects
Commit dd1c1ce6 authored by Jess's avatar Jess
Browse files

Issue #2638140 by alexpott, dawehner, cilefen, xjm, Fabianx, Xano, catch:...

Issue #2638140 by alexpott, dawehner, cilefen, xjm, Fabianx, Xano, catch: Error logging should log a backtrace consistently
parent bc834169
No related branches found
No related tags found
No related merge requests found
...@@ -80,6 +80,7 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line, $c ...@@ -80,6 +80,7 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line, $c
'%line' => $caller['line'], '%line' => $caller['line'],
'severity_level' => $severity_level, 'severity_level' => $severity_level,
'backtrace' => $backtrace, 'backtrace' => $backtrace,
'@backtrace_string' => (new \Exception())->getTraceAsString(),
), $recoverable || $to_string); ), $recoverable || $to_string);
} }
} }
...@@ -116,9 +117,9 @@ function error_displayable($error = NULL) { ...@@ -116,9 +117,9 @@ function error_displayable($error = NULL) {
* *
* @param $error * @param $error
* An array with the following keys: %type, @message, %function, %file, * An array with the following keys: %type, @message, %function, %file,
* %line, severity_level, and backtrace. All the parameters are plain-text, * %line, @backtrace_string, severity_level, and backtrace. All the parameters
* with the exception of @message, which needs to be an HTML string, and * are plain-text, with the exception of @message, which needs to be an HTML
* backtrace, which is a standard PHP backtrace. * string, and backtrace, which is a standard PHP backtrace.
* @param bool $fatal * @param bool $fatal
* TRUE for: * TRUE for:
* - An exception is thrown and not caught by something else. * - An exception is thrown and not caught by something else.
...@@ -163,18 +164,18 @@ function _drupal_log_error($error, $fatal = FALSE) { ...@@ -163,18 +164,18 @@ function _drupal_log_error($error, $fatal = FALSE) {
// installer. // installer.
if (\Drupal::hasService('logger.factory')) { if (\Drupal::hasService('logger.factory')) {
try { try {
\Drupal::logger('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error); \Drupal::logger('php')->log($error['severity_level'], '%type: @message in %function (line %line of %file) @backtrace_string.', $error);
} }
catch (\Exception $e) { catch (\Exception $e) {
// We can't log, for example because the database connection is not // We can't log, for example because the database connection is not
// available. At least try to log to PHP error log. // available. At least try to log to PHP error log.
error_log(strtr('Failed to log error: %type: @message in %function (line %line of %file).', $error)); error_log(strtr('Failed to log error: %type: @message in %function (line %line of %file). @backtrace_string', $error));
} }
} }
// Log fatal errors, so developers can find and debug them. // Log fatal errors, so developers can find and debug them.
if ($fatal) { if ($fatal) {
error_log(sprintf('%s: %s in %s on line %d', $error['%type'], $error['@message'], $error['%file'], $error['%line'])); error_log(sprintf('%s: %s in %s on line %d %s', $error['%type'], $error['@message'], $error['%file'], $error['%line'], $error['@backtrace_string']));
} }
if (PHP_SAPI === 'cli') { if (PHP_SAPI === 'cli') {
......
...@@ -71,7 +71,7 @@ public static function decodeException($exception) { ...@@ -71,7 +71,7 @@ public static function decodeException($exception) {
'%line' => $caller['line'], '%line' => $caller['line'],
'severity_level' => static::ERROR, 'severity_level' => static::ERROR,
'backtrace' => $backtrace, 'backtrace' => $backtrace,
'backtrace_string' => $exception->getTraceAsString(), '@backtrace_string' => $exception->getTraceAsString(),
); );
} }
......
...@@ -253,7 +253,7 @@ public function testLoggerException() { ...@@ -253,7 +253,7 @@ public function testLoggerException() {
// Find fatal error logged to the simpletest error.log // Find fatal error logged to the simpletest error.log
$errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); $errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
$this->assertIdentical(count($errors), 2, 'The error + the error that the logging service is broken has been written to the error log.'); $this->assertIdentical(count($errors), 8, 'The error + the error that the logging service is broken has been written to the error log.');
$this->assertTrue(strpos($errors[0], 'Failed to log error') !== FALSE, 'The error handling logs when an error could not be logged to the logger.'); $this->assertTrue(strpos($errors[0], 'Failed to log error') !== FALSE, 'The error handling logs when an error could not be logged to the logger.');
$expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php'; $expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment