You are here

function audit_log_elastic_search_audit_log in Audit Log 7

Implements hook_audit_log().

File

modules/audit_log_elastic_search/audit_log_elastic_search.module, line 12
Hook implemenations for the Audit elastic search logging module.

Code

function audit_log_elastic_search_audit_log(Auditlog $log) {
  $client_id = audit_log_elastic_search_get_cluster_id();
  if (!empty($client_id)) {
    $client = elasticsearch_connector_get_client_by_id($client_id);
    if ($client) {
      $doc = array();
      $doc['index'] = audit_log_elastic_search_get_index_name();
      $doc['type'] = audit_log_elastic_search_get_type_name();

      // Workaround to order the logs if the same timestamp based on microtime.
      $microtime_float = explode('.', microtime(TRUE));
      if (empty($microtime_float[1])) {
        $microtime = '0.0';
      }
      else {
        $microtime = '0.' . $microtime_float[1];
      }
      $doc['body'] = (array) $log;
      $doc['body']['microtime'] = (double) $microtime;
      $doc['body']['date'] = date('c', $log->timestamp);
      $doc['body']['_ttl'] = variable_get('audit_log_elastic_search_ttl', AUDIT_LOG_ELASTIC_SEARCH_DEFAULT_INTERVAL);

      // Indexing document.
      try {
        $ret = $client
          ->index($doc);
      } catch (Exception $e) {
        error_log($e
          ->getMessage());
      }
    }
  }
}