You are here

public function AcquiaPurgeDiagnostics::log in Acquia Purge 7

Log diagnostic test results to watchdog.

Parameters

array $results: Associative array with test results or an individual test result array.

bool $deduplicate: Prevent diagnostic messages from ending up in the logs multiple times when they already have been logged since the last queue wipe.

File

lib/AcquiaPurgeDiagnostics.php, line 170
Contains AcquiaPurgeExecutorsService.

Class

AcquiaPurgeDiagnostics
Provides access to the diagnostic tests.

Code

public function log(array $results, $deduplicate = TRUE) {
  $map = array(
    ACQUIA_PURGE_SEVLEVEL_INFO => WATCHDOG_INFO,
    ACQUIA_PURGE_SEVLEVEL_OK => WATCHDOG_INFO,
    ACQUIA_PURGE_SEVLEVEL_WARNING => WATCHDOG_ERROR,
    ACQUIA_PURGE_SEVLEVEL_ERROR => WATCHDOG_CRITICAL,
  );

  // Wrap single a single test result into a workable array.
  if (isset($results['severity'])) {
    $results = array(
      $results,
    );
  }

  // Iterate the items and report them to the watchdog log.
  foreach ($results as $result) {
    $descr = $result['description_plain'];
    if (empty($description)) {
      $descr = $result['value_plain'];
    }

    // If we aren't asked to deduplicate messages, log it straight away.
    if (!$deduplicate) {
      watchdog('acquia_purge', $descr, array(), $map[$result['severity']]);
    }
    else {
      $logged_errors = $this->loggedErrors
        ->get();
      $hash = sha1($descr);
      if (in_array($hash, $logged_errors)) {
        return;
      }

      // Log the message and add the hash to the deduplication list.
      watchdog('acquia_purge', $descr, array(), $map[$result['severity']]);
      $logged_errors[] = $hash;
      $this->loggedErrors
        ->set($logged_errors);
    }
  }
}