You are here

protected function Logger::enhanceLogEntry in MongoDB 8.2

Fill in the log_entry function, file, and line.

Parameters

array $entry: An event information to be logger.

array $backtrace: A call stack.

Throws

\ReflectionException

2 calls to Logger::enhanceLogEntry()
Logger::log in modules/mongodb_watchdog/src/Logger.php
MockLogger::enhanceLogEntry in modules/mongodb_watchdog/tests/src/Unit/MockLogger.php
Fill in the log_entry function, file, and line.
1 method overrides Logger::enhanceLogEntry()
MockLogger::enhanceLogEntry in modules/mongodb_watchdog/tests/src/Unit/MockLogger.php
Fill in the log_entry function, file, and line.

File

modules/mongodb_watchdog/src/Logger.php, line 238

Class

Logger
Class Logger is a PSR/3 Logger using a MongoDB data store.

Namespace

Drupal\mongodb_watchdog

Code

protected function enhanceLogEntry(array &$entry, array $backtrace) : void {

  // Create list of functions to ignore in backtrace.
  static $ignored = [
    'call_user_func_array' => 1,
    '_drupal_log_error' => 1,
    '_drupal_error_handler' => 1,
    '_drupal_error_handler_real' => 1,
    'Drupal\\mongodb_watchdog\\Logger::log' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::log' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::alert' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::critical' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::debug' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::emergency' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::error' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::info' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::notice' => 1,
    'Drupal\\Core\\Logger\\LoggerChannel::warning' => 1,
    'Psr\\Log\\AbstractLogger::alert' => 1,
    'Psr\\Log\\AbstractLogger::critical' => 1,
    'Psr\\Log\\AbstractLogger::debug' => 1,
    'Psr\\Log\\AbstractLogger::emergency' => 1,
    'Psr\\Log\\AbstractLogger::error' => 1,
    'Psr\\Log\\AbstractLogger::info' => 1,
    'Psr\\Log\\AbstractLogger::notice' => 1,
    'Psr\\Log\\AbstractLogger::warning' => 1,
  ];
  foreach ($backtrace as $bt) {
    if (isset($bt['function'])) {
      $function = empty($bt['class']) ? $bt['function'] : $bt['class'] . '::' . $bt['function'];
      if (empty($ignored[$function])) {
        $entry['%function'] = $function;

        /* Some part of the stack, like the line or file info, may be missing.
         * From research in 2021-01, this only appears to happen on PHP < 7.0.
         *
         * @see http://goo.gl/8s75df
         *
         * No need to fetch the line using reflection: it would be redundant
         * with the name of the function.
         */
        $entry['%line'] = $bt['line'] ?? NULL;
        $file = $bt['file'] ?? '';
        if (empty($file) && is_callable($function)) {
          $reflectionObj = empty($bt['class']) ? new \ReflectionFunction($function) : new \ReflectionMethod($function);
          $file = $reflectionObj
            ->getFileName();
        }
        $entry['%file'] = $file;
        break;
      }
      elseif ($bt['function'] == '_drupal_exception_handler') {
        $e = $bt['args'][0];
        $this
          ->enhanceLogEntry($entry, $e
          ->getTrace());
      }
    }
  }
}