error_log.module 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * Sends watchdog log entries to the PHP error log.
  5. *
  6. * @Author: mark burdett https://www.drupal.org/u/mfb
  7. */
  8. /**
  9. * Implements hook_form_system_logging_settings_alter().
  10. */
  11. function error_log_form_system_logging_settings_alter(array &$form, array &$form_state) {
  12. module_load_include('admin.inc', 'error_log');
  13. error_log_settings_form($form, $form_state);
  14. }
  15. /**
  16. * Implements hook_help().
  17. */
  18. function error_log_help($path, $arg) {
  19. switch ($path) {
  20. case 'admin/help#error_log':
  21. return t('Sends watchdog log entries to the PHP error log.');
  22. }
  23. }
  24. /**
  25. * Implements hook_watchdog().
  26. */
  27. function error_log_watchdog(array $log) {
  28. $levels = variable_get('error_log_levels', error_log_default_levels());
  29. if (empty($levels["level_{$log['severity']}"])) {
  30. return;
  31. }
  32. // Drush handles error logging for us, so disable redundant logging.
  33. if (function_exists('drush_main') && !ini_get('error_log')) {
  34. return;
  35. }
  36. $ignored_types = array_map('trim', preg_split('/\R/', variable_get('error_log_ignored_types', ''), -1, PREG_SPLIT_NO_EMPTY));
  37. if (in_array($log['type'], $ignored_types)) {
  38. return;
  39. }
  40. if (!is_array($log['variables'])) {
  41. $log['variables'] = array();
  42. }
  43. $severity_list = error_log_severity_levels();
  44. $message = "[{$severity_list[$log['severity']]}] [{$log['type']}] [{$log['ip']}] [uid:{$log['uid']}] [{$log['request_uri']}] [{$log['referer']}] ";
  45. // Cleanup excessive whitespace and HTML-encoded quotes.
  46. $message .= str_replace(array(' ', "\n"), '', html_entity_decode(strip_tags(strtr($log['message'], $log['variables'])), ENT_QUOTES, 'UTF-8'));
  47. return error_log($message);
  48. }
  49. /**
  50. * Provides untranslated watchdog severity levels.
  51. */
  52. function error_log_severity_levels() {
  53. return array(
  54. WATCHDOG_EMERGENCY => 'emergency',
  55. WATCHDOG_ALERT => 'alert',
  56. WATCHDOG_CRITICAL => 'critical',
  57. WATCHDOG_ERROR => 'error',
  58. WATCHDOG_WARNING => 'warning',
  59. WATCHDOG_NOTICE => 'notice',
  60. WATCHDOG_INFO => 'info',
  61. WATCHDOG_DEBUG => 'debug',
  62. );
  63. }
  64. /**
  65. * Provides default log level configuration.
  66. */
  67. function error_log_default_levels() {
  68. foreach (error_log_severity_levels() as $key => $value) {
  69. $levels["level_$key"] = "level_$key";
  70. }
  71. return $levels;
  72. }