You are here

gelf.module in GELF 7

Same filename and directory in other branches
  1. 8 gelf.module
  2. 6 gelf.module

File

gelf.module
View source
<?php

/**
 * @file
 * gelf.module
 *
 */

/**
 * Implements hook_permission()
 *
 * Allows admins to control access to gelf settings.
 */
function gelf_permission() {
  return array(
    'administer gelf' => array(
      'title' => t('administer gelf'),
      'description' => t('Manage graylog2 host settings'),
    ),
  );
}

/**
 * Implement hook_watchdog().
 */
function gelf_watchdog($entry) {
  if (!gelf_require()) {
    if (function_exists('user_access') && user_access('administer gelf')) {
      drupal_set_message(t('GELF module requires the GELF PHP library to be installed.  View the README for installation instructions.'), 'error');
    }
    return;
  }
  $host = variable_get('gelf_host', 'localhost');
  $port = variable_get('gelf_port', 12201);
  $gelf = new GELFMessage();
  $message = filter_xss(is_null($entry['variables']) ? $entry['message'] : strtr($entry['message'], $entry['variables']));
  $short_msg_length = 100;
  if (strlen($message) > $short_msg_length) {
    $short_message = preg_replace('/\\s+?(\\S+)?$/u', '', substr($message, 0, $short_msg_length));
  }
  else {
    $short_message = $message;
  }
  $username = isset($entry['user']->name) ? $entry['user']->name : variable_get('anonymous', t('Anonymous'));
  $gelf
    ->setShortMessage($short_message);
  $gelf
    ->setFullMessage($message);
  $gelf
    ->setHost(php_uname('n'));
  $gelf
    ->setFacility($entry['type']);
  $gelf
    ->setLevel($entry['severity']);
  $gelf
    ->setTimestamp($entry['timestamp']);
  $gelf
    ->setAdditional("Referer", $entry['referer']);
  $gelf
    ->setAdditional("Link", $entry['link']);
  $gelf
    ->setAdditional("Username", $username);
  $gelf
    ->setAdditional("Uid", $entry['user']->uid);
  $gelf
    ->setAdditional("Request_uri", $entry['request_uri']);
  $gelf
    ->setAdditional("Request_method", $_SERVER['REQUEST_METHOD']);
  $gelf
    ->setAdditional("Server_host", $_SERVER['HTTP_HOST']);
  $gelf
    ->setAdditional("Client_host", $entry['ip']);
  try {
    $publisher = new GELFMessagePublisher($host, $port);
    $publisher
      ->publish($gelf);
  } catch (UnexpectedValueException $e) {
    if (function_exists('user_access') && user_access('administer gelf')) {
      drupal_set_message(t('Failed to publish gelf message: %message', array(
        '%message' => $e
          ->getMessage(),
      )), 'error');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function gelf_form_system_logging_settings_alter(&$form, $form_state) {
  $form['gelf_host'] = array(
    '#type' => 'textfield',
    '#title' => t('Graylog2 host'),
    '#default_value' => variable_get('gelf_host', 'localhost'),
  );
  $form['gelf_port'] = array(
    '#type' => 'textfield',
    '#title' => t('Graylog2 GELF port'),
    '#default_value' => variable_get('gelf_port', 12201),
  );
  if (!gelf_require()) {
    drupal_set_message(t('GELF module requires the GELF PHP library to be installed.  View the README for installation instructions.'), 'error');
  }
  return $form;
}

/**
 * Include gelf-php library.
 *
 * @return boolean
 */
function gelf_require() {

  // Check if the classes already exist and allow existing autoloaders.
  if (class_exists('GELFMessage') && class_exists('GELFMessagePublisher')) {
    return TRUE;
  }

  // Check module for Composer autoload file.
  if (@(include_once dirname(__FILE__) . '/vendor/autoload.php')) {
    return TRUE;
  }

  // Use Libraries API to load it.
  if (function_exists('libraries_get_path')) {
    $gelfmsg_path = libraries_get_path('gelf-php') . '/GELFMessage.php';
    $gelfpub_path = libraries_get_path('gelf-php') . '/GELFMessagePublisher.php';

    // Check if the php-gelf library is available
    if (file_exists(DRUPAL_ROOT . '/' . $gelfmsg_path) && file_exists(DRUPAL_ROOT . '/' . $gelfpub_path)) {
      require_once DRUPAL_ROOT . '/' . $gelfmsg_path;
      require_once DRUPAL_ROOT . '/' . $gelfpub_path;
      return TRUE;
    }
  }
  return FALSE;
}

Functions