You are here

function monolog_logging_watchdog in Monolog 6

Same name and namespace in other branches
  1. 7 modules/monolog_logging/monolog_logging.module \monolog_logging_watchdog()

Implements hook_watchdog().

File

modules/monolog_logging/monolog_logging.module, line 61
Integrates Drupal's internal logging system with Monolog by routing watchdog messages to Monolog channels.

Code

function monolog_logging_watchdog(array $log_entry) {
  try {

    // Perform a partial bootstrap if watchdog is called prior to the
    // DRUPAL_BOOTSTRAP_FULL phase.
    if (!function_exists('monolog')) {
      monolog_logging_bootstrap();
    }

    // Check whether to use the watchdog type as the channel name.
    // @see http://drupal.org/node/1990282
    if (variable_get('monolog_type_as_channel', 1)) {
      $logger = clone monolog('watchdog');

      // If there was an error, the PSR-3 null logger is used which does not
      // have a setName() method. There is no need to set a name since the
      // message will be be sent to a black hole.
      if ('Psr\\Log\\NullLogger' != get_class($logger)) {
        $logger
          ->setName($log_entry['type']);
      }
    }
    else {
      $logger = monolog('watchdog');
    }
    $enabled_contexts = monolog_logging_get_contexts();
    $context = array_intersect_key($log_entry, $enabled_contexts);
    if (isset($enabled_contexts['request_id'])) {
      $context['request_id'] = monolog_request_id();
    }
    $message = strip_tags(!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables']));
    $level = monolog_logging_map_severity_level($log_entry['severity']);
    $logger
      ->log($level, $message, $context);
  } catch (Exception $e) {

    // Fail silently since we cannot log any messages or do anything that would
    // trigger another watchdog call.
  }
}