You are here

gelf.module in GELF 6

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

File

gelf.module
View source
<?php

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

/**
 * Implementation of hook_menu()
 *
 * Set up admin settings callbacks, etc.
 */
function gelf_menu() {
  $items = array();
  $items['admin/settings/logging/gelf'] = array(
    'title' => 'GELF settings',
    'description' => 'Settings for logging to Graylog2 using GELF.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'gelf_admin_settings_form',
    ),
    'access arguments' => array(
      'administer gelf',
    ),
  );
  return $items;
}

/**
 * Implemetation of hook_perm()
 *
 * Allows admins to control access to gelf settings.
 */
function gelf_perm() {
  return array(
    'administer gelf',
  );
}

/**
 * Implement hook_watchdog().
 */
function gelf_watchdog($entry) {
  static $gelf_publisher;
  if (module_exists('libraries') && ($gelflib_path = libraries_get_path('gelf-php'))) {
    if (file_exists($gelflib_path . '/GELFMessage.php') && file_exists($gelflib_path . '/GELFMessagePublisher.php')) {
      require_once $gelflib_path . '/GELFMessage.php';
      require_once $gelflib_path . '/GELFMessagePublisher.php';

      // Set up the gelf message publisher
      if (!isset($gelf_publisher)) {
        $host = variable_get('gelf_host', 'localhost');
        $port = variable_get('gelf_port', 12201);
        try {
          $gelf_publisher = new GELFMessagePublisher($host, $port);
        } catch (InvalidArgumentException $e) {
          if (user_access('administer gelf')) {
            drupal_set_message(t('Unable to instantiate GELF message publisher: @msg', array(
              '@msg' => $e
                ->getMessage(),
            )), 'error');
          }
          return;
        }
      }
      $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("Server_host", $_SERVER['HTTP_HOST']);
      $gelf
        ->setAdditional("Client_host", $entry['ip']);
      try {
        $gelf_publisher
          ->publish($gelf);
      } catch (UnexpectedValueException $e) {
        if (user_access('administer gelf')) {
          drupal_set_message(t('Failed to publish gelf message: %message', array(
            '%message' => $e
              ->getMessage(),
          )), 'error');
        }
      }
    }
  }
  else {
    if (user_access('administer gelf')) {
      drupal_set_message(t('GELF module requires libraries to be installed!'), 'error');
    }
  }
}

/**
 * Menu callback for GELF admin settings.
 */
function gelf_admin_settings_form() {
  $form = array();
  $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),
  );
  $gelflib_path = libraries_get_path('gelf-php');
  if (!file_exists($gelflib_path . '/GELFMessage.php') || !file_exists($gelflib_path . '/GELFMessagePublisher.php')) {
    drupal_set_message(t('GELF module requires the GELF PHP library to be installed.  View the README for installation instructions.'), 'error');
  }
  return system_settings_form($form);
}

Functions

Namesort descending Description
gelf_admin_settings_form Menu callback for GELF admin settings.
gelf_menu Implementation of hook_menu()
gelf_perm Implemetation of hook_perm()
gelf_watchdog Implement hook_watchdog().