Changeset 390


Ignore:
Timestamp:
Nov 18, 2011 4:01:54 AM (13 years ago)
Author:
anonymous
Message:

Made logMsg more efficient, improved message throttle functionality.

Location:
trunk/lib
Files:
3 edited

Legend:

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

    r380 r390  
    6262        // Public name and email address for this application.
    6363        'site_name' => null,
    64         'site_email' => null,
     64        'site_email' => '', // Set to no-reply@HTTP_HOST if not set here.
    6565        'site_url' => '', // URL automatically determined by _SERVER['HTTP_HOST'] if not set here.
    6666        'images_path' => '', // Location for codebase-generated interface widgets (ex: "/admin/i").
     
    126126        'log_filename' => 'app_log',
    127127
    128         // Don't email or sms duplicate messages that happen more often than this value (in seconds).
    129         'log_multiple_timeout' => 300,
     128        // Don't email or SMS duplicate messages that happen more often than this value (in seconds).
     129        'log_multiple_timeout' => 3600, // Hourly
    130130
    131131        // Logging priority can be any of the following, or false to deactivate:
     
    143143        'log_screen_priority' => false,
    144144
    145         // Email address to receive log event emails.
     145        // Email address to receive log event emails. Use multiple addresses by separating them with commas.
    146146        'log_to_email_address' => null,
    147147
     
    357357            $this->setParam(array('site_url' => sprintf('%s://%s', ('on' == getenv('HTTPS') ? 'https' : 'http'), getenv('HTTP_HOST'))));
    358358        }
     359       
     360        // In case site_email isn't set, use something halfway presentable.
     361        if (isset($_SERVER['HTTP_HOST']) && '' != $_SERVER['HTTP_HOST'] && '' == $this->getParam('site_email')) {
     362            $this->setParam(array('site_email' => sprintf('no-reply@%s', getenv('HTTP_HOST'))));
     363        }
    359364
    360365        // A key for calculating simple cryptographic signatures.
     
    525530    /**
    526531     * Logs messages to defined channels: file, email, sms, and screen. Repeated messages are
    527      * not repeated but printed once with count.
     532     * not repeated but printed once with count. Log events that match a sendable channel (email or SMS)
     533     * are sent once per 'log_multiple_timeout' setting (to avoid a flood of error emails).
    528534     *
    529535     * @access public
    530536     * @param string $message   The text description of the message.
    531537     * @param int    $priority  The type of message priority (in descending order):
    532      *                          LOG_EMERG     system is unusable
    533      *                          LOG_ALERT     action must be taken immediately
    534      *                          LOG_CRIT      critical conditions
    535      *                          LOG_ERR       error conditions
    536      *                          LOG_WARNING   warning conditions
    537      *                          LOG_NOTICE    normal, but significant, condition
    538      *                          LOG_INFO      informational message
    539      *                          LOG_DEBUG     debug-level message
     538     *                          LOG_EMERG     0 system is unusable
     539     *                          LOG_ALERT     1 action must be taken immediately
     540     *                          LOG_CRIT      2 critical conditions
     541     *                          LOG_ERR       3 error conditions
     542     *                          LOG_WARNING   4 warning conditions
     543     *                          LOG_NOTICE    5 normal, but significant, condition
     544     *                          LOG_INFO      6 informational message
     545     *                          LOG_DEBUG     7 debug-level message
    540546     * @param string $file      The file where the log event occurs.
    541547     * @param string $line      The line of the file where the log event occurs.
     
    556562            // We must use trigger_error to report this problem rather than calling $app->logMsg, which might lead to an infinite loop.
    557563            trigger_error(sprintf('Codebase error: log directory (%s) not found or writable.', $this->getParam('log_directory')), E_USER_NOTICE);
     564        }
     565       
     566        // Before we get any further, let's see if ANY log events are configured to be reported.
     567        if ((false === $this->getParam('log_file_priority') || $priority > $this->getParam('log_file_priority'))
     568        && (false === $this->getParam('log_email_priority') || $priority > $this->getParam('log_email_priority'))
     569        && (false === $this->getParam('log_sms_priority') || $priority > $this->getParam('log_sms_priority'))
     570        && (false === $this->getParam('log_screen_priority') || $priority > $this->getParam('log_screen_priority'))) {
     571            // This event would not be recorded, skip it entirely.
     572            return false;
    558573        }
    559574
     
    578593        }
    579594
    580         // Use "lock" files to prevent sending email and sms notices ad infinitum.
    581         $site_hash = md5($_SERVER['SCRIPT_NAME']);
    582         $temp_dir = $this->getParam('tmp_dir') . "/codebase_msgs_$site_hash/";
    583         $temp_file = $temp_dir . md5($file . $line); // Just use the file and line for the msg_id to limit the number of possible messages.
    584         if (!is_dir($temp_dir)) {
    585             mkdir($temp_dir);
    586         }
    587         $send_notifications = true;
    588         if (is_file($temp_file)) {
    589             $msg_last_sent = filectime($temp_file);
    590             // Has this message been sent more recently than the timeout?
    591             if ((time() - $msg_last_sent) <= $this->getParam('log_multiple_timeout')) {
    592                 // This message was already sent recently.
    593                 $send_notifications = false;
     595        // For email and SMS notification types use "lock" files to prevent sending email and SMS notices ad infinitum.
     596        if ((false !== $this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority'))
     597        || (false !== $this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority'))) {
     598            // This event will generate a "send" notification. Prepare lock file.
     599            $site_hash = md5(empty($_SERVER['SERVER_NAME']) ? $_SERVER['SCRIPT_FILENAME'] : $_SERVER['SERVER_NAME']);
     600            $lock_dir = $this->getParam('tmp_dir') . "/codebase_msgs_$site_hash/";
     601            // Just use the file and line for the msg_id to limit the number of possible messages
     602            // (the message string itself shan't be used as it may contain innumerable combinations).
     603            $lock_file = $lock_dir . md5($file . ':' . $line);
     604            if (!is_dir($lock_dir)) {
     605                mkdir($lock_dir);
     606            }
     607            $send_notifications = true;
     608            if (is_file($lock_file)) {
     609                $msg_last_sent = filectime($lock_file);
     610                // Has this message been sent more recently than the timeout?
     611                if ((time() - $msg_last_sent) <= $this->getParam('log_multiple_timeout')) {
     612                    // This message was already sent recently.
     613                    $send_notifications = false;
     614                } else {
     615                    // Timeout has expired; send notifications again and reset timeout.
     616                    touch($lock_file);
     617                }
    594618            } else {
    595                 // Timeout has expired go ahead and send notifications again.
    596                 if (file_exists($temp_file)) {
    597                     unlink($temp_file);
    598                 }
    599             }
    600         } else {
    601             touch($temp_file);
    602         }
    603        
     619                touch($lock_file);
     620            }
     621        }
    604622       
    605623        // Data to be stored for a log event.
     
    615633
    616634        // FILE ACTION
    617         if ($this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) {
     635        if (false !== $this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) {
    618636            $event_str = '[' . join('] [', $event) . ']';
    619637            error_log(mb_substr($event_str, 0, 1024) . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename'));
    620638        }
    621639
    622         // NOTIFY SOMEONE
    623         if ($send_notifications) {
    624             // EMAIL ACTION
    625             if ($this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority')) {
    626                 $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], $message);
    627                 $email_msg = sprintf("A %s log event occured on %s\n\n", $event['type'], getenv('HTTP_HOST'));
    628                 $headers = "From: codebase@strangecode.com";
    629                 foreach ($event as $k=>$v) {
    630                     $email_msg .= sprintf("%-11s%s\n", $k, $v);
    631                 }
    632                 mb_send_mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers);
    633             }
    634    
    635             // SMS ACTION
    636             if ($this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority')) {
    637                 $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority);
    638                 $sms_msg = sprintf('%s [%s:%s]', mb_substr($event['message'], 0, 64), basename($file), $line);
    639                 $headers = "From: codebase@strangecode.com";
    640                 mb_send_mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers);
    641             }
     640        // EMAIL ACTION
     641        if (false !== $this->getParam('log_email_priority') && $priority <= $this->getParam('log_email_priority') && $send_notifications) {
     642            $subject = sprintf('[%s %s] %s', getenv('HTTP_HOST'), $event['type'], mb_substr($message, 0, 64));
     643            $email_msg = sprintf("A %s log event occurred on %s\n\n", $event['type'], getenv('HTTP_HOST'));
     644            $headers = 'From: ' . $this->getParam('site_email');
     645            foreach ($event as $k=>$v) {
     646                $email_msg .= sprintf("%-11s%s\n", $k, $v);
     647            }
     648            mb_send_mail($this->getParam('log_to_email_address'), $subject, $email_msg, $headers);
     649        }
     650
     651        // SMS ACTION
     652        if (false !== $this->getParam('log_sms_priority') && $priority <= $this->getParam('log_sms_priority') && $send_notifications) {
     653            $subject = sprintf('[%s %s]', getenv('HTTP_HOST'), $priority);
     654            $sms_msg = sprintf('%s [%s:%s]', mb_substr($event['message'], 0, 64), basename($file), $line);
     655            $headers = 'From: ' . $this->getParam('site_email');
     656            mb_send_mail($this->getParam('log_to_sms_address'), $subject, $sms_msg, $headers);
    642657        }
    643658   
    644659        // SCREEN ACTION
    645         if ($this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) {
     660        if (false !== $this->getParam('log_screen_priority') && $priority <= $this->getParam('log_screen_priority')) {
    646661            echo "[{$event['type']}] [{$event['message']}]\n";
    647662        }
     
    649664        // Restore original locale.
    650665        setlocale(LC_TIME, $locale);
     666       
     667        return true;
    651668    }
    652669
  • trunk/lib/Cart.inc.php

    r376 r390  
    279279    function sum($key='extended_price')
    280280    {
    281         global $app;
    282 
    283281        $sum = 0;
    284282        switch ($key) {
     
    294292
    295293        default :
    296             // Retreive arbitrary values stored in the cart (shipping, air miles, etc).
     294            // Retrieve arbitrary values stored in the cart (shipping, air miles, etc).
    297295            foreach ($_SESSION['_cart'][$this->_ns]['items'] as $item_id => $specs) {
    298296                $sum += isset($specs[$key]) && is_numeric($specs[$key]) ? $specs[$key] * $specs['quantity'] : 0;
  • trunk/lib/Hierarchy.inc.php

    r376 r390  
    122122                $child_id =& $this->child_id;
    123123            } else {
    124                 $app->logMsg(_("toStringID failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     124                $app->logMsg('toStringID failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    125125                return false;
    126126            }
     
    154154            return array('node_type' => $node_parts[1], 'node_id' => $node_parts[2]);
    155155        } else {
    156             $app->logMsg(_("Cannot parse node identifier, not formated correctly."), LOG_ERR, __FILE__, __LINE__);
     156            $app->logMsg('Cannot parse node identifier, not formated correctly.'), LOG_ERR, __FILE__, __LINE__);
    157157            return false;
    158158        }
     
    177177                $child_id =& $this->child_id;
    178178            } else {
    179                 $app->logMsg(_("insertNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     179                $app->logMsg('insertNode failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    180180                return false;
    181181            }
     
    256256                $child_id =& $this->child_id;
    257257            } else {
    258                 $app->logMsg(_("deleteNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     258                $app->logMsg('deleteNode failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    259259                return false;
    260260            }
     
    306306                $child_id =& $this->child_id;
    307307            } else {
    308                 $app->logMsg(_("moveNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     308                $app->logMsg('moveNode failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    309309                return false;
    310310            }
     
    383383                $child_id =& $this->child_id;
    384384            } else {
    385                 $app->logMsg(_("getParents failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     385                $app->logMsg('getParents failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    386386                return false;
    387387            }
     
    434434                $child_id =& $this->child_id;
    435435            } else {
    436                 $app->logMsg(_("getNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     436                $app->logMsg('getNode failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    437437                return false;
    438438            }
     
    478478                $child_id =& $this->child_id;
    479479            } else {
    480                 $app->logMsg(_("getChildren failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     480                $app->logMsg('getChildren failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    481481                return false;
    482482            }
     
    530530                $child_id =& $this->child_id;
    531531            } else {
    532                 $app->logMsg(_("getNumberChildren failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     532                $app->logMsg('getNumberChildren failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    533533                return false;
    534534            }
     
    570570                $child_id =& $this->child_id;
    571571            } else {
    572                 $app->logMsg(_("isLeaf failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     572                $app->logMsg('isLeaf failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    573573                return false;
    574574            }
     
    686686                $child_id =& $this->child_id;
    687687            } else {
    688                 $app->logMsg(_("nodeExists failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     688                $app->logMsg('nodeExists failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    689689                return false;
    690690            }
     
    740740                $child_id =& $this->child_id;
    741741            } else {
    742                 $app->logMsg(_("getNodeList failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__);
     742                $app->logMsg('getNodeList failed. Arguments not specified properly.'), LOG_ERR, __FILE__, __LINE__);
    743743                return false;
    744744            }
Note: See TracChangeset for help on using the changeset viewer.