You are here

class NewRelicLogger in New Relic 8

Same name and namespace in other branches
  1. 2.x src/Logger/NewRelicLogger.php \Drupal\new_relic_rpm\Logger\NewRelicLogger
  2. 2.0.x src/Logger/NewRelicLogger.php \Drupal\new_relic_rpm\Logger\NewRelicLogger

A Logger that allows sending messages to the New Relic API.

Hierarchy

  • class \Drupal\new_relic_rpm\Logger\NewRelicLogger implements \Psr\Log\LoggerInterface uses \Psr\Log\LoggerTrait

Expanded class hierarchy of NewRelicLogger

1 file declares its use of NewRelicLogger
NewRelicLoggerTest.php in tests/src/Unit/Logger/NewRelicLoggerTest.php
1 string reference to 'NewRelicLogger'
new_relic_rpm.services.yml in ./new_relic_rpm.services.yml
new_relic_rpm.services.yml
1 service uses NewRelicLogger
new_relic_rpm.logger in ./new_relic_rpm.services.yml
Drupal\new_relic_rpm\Logger\NewRelicLogger

File

src/Logger/NewRelicLogger.php, line 15

Namespace

Drupal\new_relic_rpm\Logger
View source
class NewRelicLogger implements LoggerInterface {
  use LoggerTrait;

  /**
   * The message's placeholders parser.
   *
   * @var \Drupal\Core\Logger\LogMessageParserInterface
   */
  protected $parser;

  /**
   * The Adapter for the New Relic extension.
   *
   * @var \Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface
   */
  protected $adapter;

  /**
   * The configuration factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The level of the last logged message.
   *
   * @var int
   */
  protected $lastLoggedLevel = 8;

  /**
   * Constructs a DbLog object.
   *
   * @param \Drupal\Core\Logger\LogMessageParserInterface $parser
   *   The parser to use when extracting message variables.
   * @param \Drupal\new_relic_rpm\ExtensionAdapter\NewRelicAdapterInterface $adapter
   *   The new relic adapter.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory used to read new relic settings.
   */
  public function __construct(LogMessageParserInterface $parser, NewRelicAdapterInterface $adapter, ConfigFactoryInterface $config_factory) {
    $this->parser = $parser;
    $this->adapter = $adapter;
    $this->configFactory = $config_factory;
  }

  /**
   * Check whether we should log the message or not based on the level.
   *
   * We exclude logging to certain levels through configuration, but we also
   * send only the latest most severe message we receive. This is because
   * Newrelic only supports setting one newrelic_notice_error().
   *
   * @param int $level
   *   The RFC 5424 log level.
   *
   * @return bool
   *   Indicator of whether the message should be logged or not.
   */
  private function shouldLog($level) {

    // Always log the most severe latest message.
    if ($level > $this->lastLoggedLevel) {
      return FALSE;
    }
    $validLevels = $this->configFactory
      ->get('new_relic_rpm.settings')
      ->get('watchdog_severities') ?: [];
    return in_array($level, $validLevels);
  }

  /**
   * Get a human readable severity name for an RFC log level.
   *
   * @param int $level
   *   The RFC 5424 log level.
   *
   * @return string
   *   The human readable severity name.
   */
  private function getSeverityName($level) {
    $levels = RfcLogLevel::getLevels();
    if (isset($levels[$level])) {
      return $levels[$level]
        ->getUntranslatedString();
    }
    return 'Unknown';
  }

  /**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {

    // Check if the severity is supposed to be logged.
    if (!$this
      ->shouldLog($level)) {
      return;
    }
    $this->lastLoggedLevel = $level;

    // If we were passed an exception, use that instead.
    if (isset($context['exception'])) {
      $this->adapter
        ->logException($context['exception']);
      return;
    }
    $format = "@message | Severity: (@severity) @severity_desc | Type: @type | Request URI: @request_uri | Referrer URI: @referer_uri | User: @uid | IP Address: @ip";
    $message_placeholders = $this->parser
      ->parseMessagePlaceholders($message, $context);
    $message = strtr($format, [
      '@severity' => $level,
      '@severity_desc' => $this
        ->getSeverityName($level),
      '@type' => $context['channel'],
      '@ip' => $context['ip'],
      '@request_uri' => $context['request_uri'],
      '@referer_uri' => $context['referer'],
      '@uid' => $context['uid'],
      '@message' => strip_tags(strtr($message, $message_placeholders)),
    ]);
    $this->adapter
      ->logError($message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NewRelicLogger::$adapter protected property The Adapter for the New Relic extension.
NewRelicLogger::$configFactory protected property The configuration factory service.
NewRelicLogger::$lastLoggedLevel protected property The level of the last logged message.
NewRelicLogger::$parser protected property The message's placeholders parser.
NewRelicLogger::getSeverityName private function Get a human readable severity name for an RFC log level.
NewRelicLogger::log public function Logs with an arbitrary level.
NewRelicLogger::shouldLog private function Check whether we should log the message or not based on the level.
NewRelicLogger::__construct public function Constructs a DbLog object.