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

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. define('DEFAULT_SYSLOG_FACILITY', LOG_LOCAL0);
  8. }
  9. else {
  10. define('DEFAULT_SYSLOG_FACILITY', LOG_USER);
  11. }
  12. /**
  13. * Implementation of hook_help().
  14. */
  15. function syslog_help($path, $arg) {
  16. switch ($path) {
  17. case 'admin/help#syslog':
  18. $output = '<p>'. t("The syslog module enables Drupal to send messages to the operating system's logging facility.") .'</p>';
  19. $output .= '<p>'. t('Syslog is an operating system administrative logging tool, and 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. On UNIX/Linux systems, the file /etc/syslog.conf defines this routing configuration; on Microsoft Windows, all messages are sent to the Event Log. For more information on syslog facilities, severity levels, and how to set up a syslog.conf file, see <a href="@syslog_conf">UNIX/Linux syslog.conf</a> and PHP\'s <a href="@php_openlog">openlog</a> and <a href="@php_syslog">syslog</a> functions.', array('@syslog_conf' => url('http://www.rt.com/man/syslog.5.html'), '@php_openlog' => url('http://www.php.net/manual/en/function.openlog.php'), '@php_syslog' => url('http://www.php.net/manual/en/function.syslog.php'))) .'</p>';
  20. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@syslog">Syslog module</a>.', array('@syslog' => 'http://drupal.org/handbook/modules/syslog')) .'</p>';
  21. return $output;
  22. }
  23. }
  24. function syslog_menu() {
  25. $items['admin/settings/logging/syslog'] = array(
  26. 'title' => 'Syslog',
  27. 'description' => 'Settings for syslog logging. Syslog is an operating system administrative logging tool used in systems 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.',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('syslog_admin_settings'),
  30. 'access arguments' => array('administer site configuration'),
  31. );
  32. return $items;
  33. }
  34. function syslog_admin_settings() {
  35. $form['syslog_identity'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t('Syslog identity'),
  38. '#default_value' => variable_get('syslog_identity', 'drupal'),
  39. '#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. For more information on syslog, see <a href="@syslog_help">Syslog help</a>.', array(
  40. '@syslog_help' => url('admin/help/syslog'))),
  41. );
  42. $form['syslog_facility'] = array(
  43. '#type' => 'select',
  44. '#title' => t('Send events to this syslog facility'),
  45. '#default_value' => variable_get('syslog_facility', DEFAULT_SYSLOG_FACILITY),
  46. '#options' => syslog_facility_list(),
  47. '#description' => t('Select the syslog facility code under which Drupal\'s messages should be sent. On UNIX/Linux systems, Drupal can flag its messages with the code LOG_LOCAL0 through LOG_LOCAL7; for Microsoft Windows, all messages are flagged with the code LOG_USER. Depending on the system configuration, syslog and other logging tools use this code to identify or filter Drupal messages from within the entire system log. For more information on syslog, see <a href="@syslog_help">Syslog help</a>.', array(
  48. '@syslog_help' => url('admin/help/syslog'))),
  49. );
  50. return system_settings_form($form);
  51. }
  52. function syslog_facility_list() {
  53. $facility_list = array(
  54. LOG_USER => t('LOG_USER - User level messages. Use this for Windows.'),
  55. );
  56. if (defined('LOG_LOCAL0')) {
  57. $facility_list += array(
  58. LOG_LOCAL0 => t('LOG_LOCAL0 - Local 0'),
  59. LOG_LOCAL1 => t('LOG_LOCAL1 - Local 1'),
  60. LOG_LOCAL2 => t('LOG_LOCAL2 - Local 2'),
  61. LOG_LOCAL3 => t('LOG_LOCAL3 - Local 3'),
  62. LOG_LOCAL4 => t('LOG_LOCAL4 - Local 4'),
  63. LOG_LOCAL5 => t('LOG_LOCAL5 - Local 5'),
  64. LOG_LOCAL6 => t('LOG_LOCAL6 - Local 6'),
  65. LOG_LOCAL7 => t('LOG_LOCAL7 - Local 7'),
  66. );
  67. }
  68. return $facility_list;
  69. }
  70. function syslog_watchdog($entry) {
  71. static $log_init = FALSE;
  72. if (!$log_init) {
  73. $log_init = TRUE;
  74. openlog(variable_get('syslog_identity', 'drupal'), LOG_NDELAY, variable_get('syslog_facility', DEFAULT_SYSLOG_FACILITY));
  75. }
  76. syslog($entry['severity'], theme('syslog_format', $entry));
  77. }
  78. function syslog_theme() {
  79. return array(
  80. 'syslog_format' => array(
  81. 'arguments' => array('entry' => NULL),
  82. ),
  83. );
  84. }
  85. /**
  86. * Format a system log entry.
  87. *
  88. * @ingroup themeable
  89. */
  90. function theme_syslog_format($entry) {
  91. global $base_url;
  92. $message = $base_url;
  93. $message .= '|'. $entry['timestamp'];
  94. $message .= '|'. $entry['type'];
  95. $message .= '|'. $entry['ip'];
  96. $message .= '|'. $entry['request_uri'];
  97. $message .= '|'. $entry['referer'];
  98. $message .= '|'. $entry['user']->uid;
  99. $message .= '|'. strip_tags($entry['link']);
  100. $message .= '|'. strip_tags(is_null($entry['variables']) ? $entry['message'] : strtr($entry['message'], $entry['variables']));
  101. return $message;
  102. }

Comments

If you have code which changes the theme by setting $GLOBALS['custom_theme'] it must get called before any watchdog messages are logged or you'll get the default theme instead of the custom theme.

This is because syslog_watchdog() calls theme() which initialises the theme system (init_theme()) if it hasn't already been initialised. init_theme() is where $GLOBALS['custom_theme'] is applied - setting it after init_theme() has run will have no effect.

Anything which generates a watchdog message (and depending on your logging level this can include PHP notices) can cause init_theme() to run before your code has a chance to override the theme.