syslog.module

You are here

  1. 7 modules/syslog/syslog.module
  2. 6 modules/syslog/syslog.module
  3. 8 core/modules/syslog/syslog.module

Redirects logging messages to syslog.

Functions

Namesort descending Description
syslog_facility_list Lists all possible syslog facilities for UNIX/Linux.
syslog_form_system_logging_settings_alter Implements hook_form_FORM_ID_alter().
syslog_help Implements hook_help().
syslog_watchdog Implements hook_watchdog().

File

modules/syslog/syslog.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Redirects logging messages to syslog.
  5. */
  6. if (defined('LOG_LOCAL0')) {
  7. /**
  8. * Sets the proper logging facility.
  9. *
  10. * Note that LOG_LOCAL0 through LOG_LOCAL7 are not available on Windows, so we
  11. * check for availability. If LOG_LOCAL0 is defined by the PHP environment, we
  12. * set that as the default; if not, we use LOG_USER.
  13. *
  14. * @see http://php.net/manual/function.syslog.php
  15. */
  16. define('DEFAULT_SYSLOG_FACILITY', LOG_LOCAL0);
  17. }
  18. else {
  19. define('DEFAULT_SYSLOG_FACILITY', LOG_USER);
  20. }
  21. /**
  22. * Implements hook_help().
  23. */
  24. function syslog_help($path, $arg) {
  25. switch ($path) {
  26. case 'admin/help#syslog':
  27. $output = '';
  28. $output .= '<h3>' . t('About') . '</h3>';
  29. $output .= '<p>' . t("The Syslog module logs events by sending messages to the logging facility of your web server's operating system. Syslog is an operating system administrative logging tool that provides valuable information for use in system management and security auditing. Most suited to medium and large sites, Syslog provides filtering tools that allow messages to be routed by type and severity. For more information, see the online handbook entry for <a href='@syslog'>Syslog module</a> and PHP's <a href='@php_openlog'>openlog</a> and <a href='@php_syslog'>syslog</a> functions.", array('@syslog' => 'http://drupal.org/documentation/modules/syslog', '@php_openlog' => 'http://www.php.net/manual/function.openlog.php', '@php_syslog' => 'http://www.php.net/manual/function.syslog.php')) . '</p>';
  30. $output .= '<h3>' . t('Uses') . '</h3>';
  31. $output .= '<dl>';
  32. $output .= '<dt>' . t('Logging for UNIX, Linux, and Mac OS X') . '</dt>';
  33. $output .= '<dd>' . t('On UNIX, Linux, and Mac OS X, the file <em>/etc/syslog.conf</em> defines the routing configuration. Messages can be flagged with the codes <code>LOG_LOCAL0</code> through <code>LOG_LOCAL7</code>. For information on Syslog facilities, severity levels, and how to set up <em>syslog.conf</em>, see the <em>syslog.conf</em> manual page on your command line.') . '</dd>';
  34. $output .= '<dt>' . t('Logging for Microsoft Windows') . '</dt>';
  35. $output .= '<dd>' . t('On Microsoft Windows, messages are always sent to the Event Log using the code <code>LOG_USER</code>.') . '</dd>';
  36. $output .= '</dl>';
  37. return $output;
  38. }
  39. }
  40. /**
  41. * Implements hook_form_FORM_ID_alter().
  42. */
  43. function syslog_form_system_logging_settings_alter(&$form, &$form_state) {
  44. $help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL;
  45. $form['syslog_identity'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Syslog identity'),
  48. '#default_value' => variable_get('syslog_identity', 'drupal'),
  49. '#description' => t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help,
  50. );
  51. if (defined('LOG_LOCAL0')) {
  52. $form['syslog_facility'] = array(
  53. '#type' => 'select',
  54. '#title' => t('Syslog facility'),
  55. '#default_value' => variable_get('syslog_facility', LOG_LOCAL0),
  56. '#options' => syslog_facility_list(),
  57. '#description' => t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help,
  58. );
  59. }
  60. $form['syslog_format'] = array(
  61. '#type' => 'textarea',
  62. '#title' => t('Syslog format'),
  63. '#default_value' => variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'),
  64. '#description' => t('Specify the format of the syslog entry. Available variables are: <dl><dt><code>!base_url</code></dt><dd>Base URL of the site.</dd><dt><code>!timestamp</code></dt><dd>Unix timestamp of the log entry.</dd><dt><code>!type</code></dt><dd>The category to which this message belongs.</dd><dt><code>!ip</code></dt><dd>IP address of the user triggering the message.</dd><dt><code>!request_uri</code></dt><dd>The requested URI.</dd><dt><code>!referer</code></dt><dd>HTTP Referer if available.</dd><dt><code>!uid</code></dt><dd>User ID.</dd><dt><code>!link</code></dt><dd>A link to associate with the message.</dd><dt><code>!message</code></dt><dd>The message to store in the log.</dd></dl>'),
  65. );
  66. $form['actions']['#weight'] = 1;
  67. }
  68. /**
  69. * Lists all possible syslog facilities for UNIX/Linux.
  70. *
  71. * @return array
  72. * An array of syslog facilities for UNIX/Linux.
  73. */
  74. function syslog_facility_list() {
  75. return array(
  76. LOG_LOCAL0 => 'LOG_LOCAL0',
  77. LOG_LOCAL1 => 'LOG_LOCAL1',
  78. LOG_LOCAL2 => 'LOG_LOCAL2',
  79. LOG_LOCAL3 => 'LOG_LOCAL3',
  80. LOG_LOCAL4 => 'LOG_LOCAL4',
  81. LOG_LOCAL5 => 'LOG_LOCAL5',
  82. LOG_LOCAL6 => 'LOG_LOCAL6',
  83. LOG_LOCAL7 => 'LOG_LOCAL7',
  84. );
  85. }
  86. /**
  87. * Implements hook_watchdog().
  88. */
  89. function syslog_watchdog(array $log_entry) {
  90. global $base_url;
  91. $log_init = &drupal_static(__FUNCTION__, FALSE);
  92. if (!$log_init) {
  93. $log_init = TRUE;
  94. $default_facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER;
  95. openlog(variable_get('syslog_identity', 'drupal'), LOG_NDELAY, variable_get('syslog_facility', $default_facility));
  96. }
  97. $message = strtr(variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'), array(
  98. '!base_url' => $base_url,
  99. '!timestamp' => $log_entry['timestamp'],
  100. '!type' => $log_entry['type'],
  101. '!ip' => $log_entry['ip'],
  102. '!request_uri' => $log_entry['request_uri'],
  103. '!referer' => $log_entry['referer'],
  104. '!uid' => $log_entry['uid'],
  105. '!link' => strip_tags($log_entry['link']),
  106. '!message' => strip_tags(!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables'])),
  107. ));
  108. syslog($log_entry['severity'], $message);
  109. }