Changeset 341 for trunk/lib


Ignore:
Timestamp:
Feb 5, 2009 10:30:54 PM (15 years ago)
Author:
dan
Message:

Added throttling to the logMsg function for SMS and EMAIL alearts. No need to send the same error to a human multiple times per minute. log_mutiple_timeout is used to cotrolhow long to time a message out for. Default is 60 seconds. So multiple emails and sms's will not be sent if the same msg happens more offten than every minute.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r334 r341  
    106106        'log_filename' => 'app_log',
    107107
     108        //Don't email or sms duplicate messages that happen more often than this value in sceonds
     109        'log_multiple_timeout' => 60,
     110
    108111        // Logging priority can be any of the following, or false to deactivate:
    109112        // LOG_EMERG     system is unusable
     
    116119        // LOG_DEBUG     debug-level message
    117120        'log_file_priority' => LOG_INFO,
    118         'log_email_priority' => false,
     121        'log_email_priority' => LOG_INFO,
    119122        'log_sms_priority' => false,
    120123        'log_screen_priority' => false,
    121124
    122125        // Email address to receive log event emails.
    123         'log_to_email_address' => null,
     126        'log_to_email_address' => 'dan@strangecode.com',
    124127
    125128        // SMS Email address to receive log event SMS messages.
     
    548551            $previous_events[$msg_id] = 1;
    549552        }
     553
     554        // Create tmp files so that we don't email and sms redundantly.
     555        $site_hash = md5(SITE_BASE);
     556        $temp_dir = "/tmp/codebase_msgs_$site_hash/";
     557        $temp_file = $temp_dir . $msg_id;
     558        if (!is_dir($temp_dir)) {
     559            mkdir($temp_dir);
     560        }
     561        $send_notifications = true;
     562        if (is_file($temp_file)) {
     563            $msg_last_sent = filectime($temp_file);
     564            //Has this message been sent more recently than the timeout?
     565            if ((time() - $msg_last_sent) < $this->getParam('log_multiple_timeout')) {
     566                //this message was alreay sent recently
     567                $send_notifications = false;
     568            } else {
     569                //timeout has expired go ahead and send notifications again
     570                unlink($temp_file);
     571            }
     572        } else {
     573            touch($temp_file);
     574        }
     575       
    550576       
    551577        // Data to be stored for a log event.
     
    566592        }
    567593
    568         // EMAIL ACTION
    569         if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) {
    570             $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message);
    571             $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST'));
    572             $headers = "From: codebase@strangecode.com";
    573             foreach ($event as $k=>$v) {
    574                 $email_msg .= sprintf("%-11s%s\n", $k, $v);
    575             }
    576             mb_send_mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com');
    577         }
    578 
    579         // SMS ACTION
    580         if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) {
    581             $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority);
    582             $sms_msg = sprintf('%s [%s:%s]', mb_substr($event['message'], 0, 64), basename($file), $line);
    583             $headers = "From: codebase@strangecode.com";
    584             mb_send_mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com');
    585         }
    586 
     594        // NOTIFY SOMEONE
     595        if ($send_notifications) {
     596
     597            // EMAIL ACTION
     598            if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) {
     599                $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message);
     600                $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST'));
     601                $headers = "From: codebase@strangecode.com";
     602                foreach ($event as $k=>$v) {
     603                    $email_msg .= sprintf("%-11s%s\n", $k, $v);
     604                }
     605                mb_send_mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers, '-f codebase@strangecode.com');
     606            }
     607   
     608            // SMS ACTION
     609            if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) {
     610                $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority);
     611                $sms_msg = sprintf('%s [%s:%s]', mb_substr($event['message'], 0, 64), basename($file), $line);
     612                $headers = "From: codebase@strangecode.com";
     613                mb_send_mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers, '-f codebase@strangecode.com');
     614            }
     615
     616        }
     617   
    587618        // SCREEN ACTION
    588619        if ($this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) {
Note: See TracChangeset for help on using the changeset viewer.