Commit dd1c1ce6 authored by xjm's avatar xjm

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
......@@ -80,6 +80,7 @@ function _drupal_error_handler_real($error_level, $message, $filename, $line, $c
'%line' => $caller['line'],
'severity_level' => $severity_level,
'backtrace' => $backtrace,
'@backtrace_string' => (new \Exception())->getTraceAsString(),
), $recoverable || $to_string);
}
}
......@@ -116,9 +117,9 @@ function error_displayable($error = NULL) {
*
* @param $error
* An array with the following keys: %type, @message, %function, %file,
* %line, severity_level, and backtrace. All the parameters are plain-text,
* with the exception of @message, which needs to be an HTML string, and
* backtrace, which is a standard PHP backtrace.
* %line, @backtrace_string, severity_level, and backtrace. All the parameters
* are plain-text, with the exception of @message, which needs to be an HTML
* string, and backtrace, which is a standard PHP backtrace.
* @param bool $fatal
* TRUE for:
* - An exception is thrown and not caught by something else.
......@@ -163,18 +164,18 @@ function _drupal_log_error($error, $fatal = FALSE) {
// installer.
if (\Drupal::hasService('logger.factory')) {
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) {
// We can't log, for example because the database connection is not
// 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.
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') {
......
......@@ -71,7 +71,7 @@ public static function decodeException($exception) {
'%line' => $caller['line'],
'severity_level' => static::ERROR,
'backtrace' => $backtrace,
'backtrace_string' => $exception->getTraceAsString(),
'@backtrace_string' => $exception->getTraceAsString(),
);
}
......
......@@ -253,7 +253,7 @@ public function testLoggerException() {
// Find fatal error logged to the simpletest 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.');
$expected_path = \Drupal::root() . '/core/modules/system/tests/modules/error_service_test/src/MonkeysInTheControlRoom.php';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment