Changeset 44 for trunk/lib/App.inc.php


Ignore:
Timestamp:
Dec 19, 2005 7:10:45 AM (18 years ago)
Author:
scdev
Message:

${1}

File:
1 edited

Legend:

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

    r43 r44  
    367367
    368368        // Save message in session under unique key to avoid duplicate messages.
    369         $_SESSION[$this->app]['messages'][md5($type . $message . $file . $line)] = array(
     369        $msg_id = md5($type . $message . $file . $line);
     370        $_SESSION[$this->app]['messages'][$msg_id] = array(
    370371            'type'    => $type,
    371372            'message' => $message,
    372373            'file'    => $file,
    373             'line'    => $line
     374            'line'    => $line,
     375            'count'   => (isset($_SESSION[$this->app]['messages'][$msg_id]['count']) ? (1 + $_SESSION[$this->app]['messages'][$msg_id]['count']) : 1)
    374376        );
    375377
     
    425427
    426428    /**
    427      * Logs a message to a user defined log file. Additional actions to take for
    428      * different types of message types can be specified (ERROR, NOTICE, etc).
     429     * Logs messages to defined channels: file, email, sms, and screen. Repeated messages are
     430     * not repeated but printed once with count.
    429431     *
    430432     * @access public
    431      *
    432433     * @param string $message   The text description of the message.
    433434     * @param int    $priority  The type of message priority (in descending order):
     
    445446    function logMsg($message, $priority=LOG_INFO, $file=null, $line=null)
    446447    {
     448        static $previous_events = array();
     449
    447450        if (!isset($this) || !is_a($this, 'App')) {
    448451            $this =& App::getInstance();
     
    466469        setlocale(LC_TIME, 'C');
    467470
     471        // Strip HTML tags except any with more than 7 characters because that's probably not a HTML tag, e.g. <email@address.com>.
     472        preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow);
     473        $message = strip_tags(preg_replace('/\s+/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null));
     474
     475        // Store this event under a unique key, counting each time it occurs so that it only gets reported a limited number of times.
     476        $msg_id = md5($message . $priority . $file . $line);
     477        if (isset($previous_events[$msg_id])) {
     478            $previous_events[$msg_id]++;
     479            if ($previous_events[$msg_id] == 2) {
     480                $this->logMsg(sprintf('%s (Event repeated more than %s times)', $message, $previous_events[$msg_id]), $priority, $file, $line);
     481            }
     482            return false;
     483        } else {
     484            $previous_events[$msg_id] = 1;
     485        }
     486       
    468487        // Data to be stored for a log event.
    469         $event = array();
    470         $event['date'] = date('Y-m-d H:i:s');
    471         $event['remote ip'] = getRemoteAddr();
    472         if (substr(PHP_OS, 0, 3) != 'WIN') {
    473             $event['pid'] = posix_getpid();
    474         }
    475         $event['type'] = $this->logPriorityToString($priority);
    476         $event['file:line'] = "$file : $line";
    477         preg_match_all('/(<[^>\s]{7,})[^>]*>/', $message, $strip_tags_allow); // <...> with lots of chars maybe we don't want stripped.
    478         $event['message'] = strip_tags(preg_replace('/\s+/', ' ', $message), (!empty($strip_tags_allow[1]) ? join('> ', $strip_tags_allow[1]) . '>' : null));
    479         $event_str = '[' . join('] [', $event) . ']';
     488        $event = array(
     489            'date'      => date('Y-m-d H:i:s'),
     490            'remote ip' => getRemoteAddr(),
     491            'pid'       => (substr(PHP_OS, 0, 3) != 'WIN' ? posix_getpid() : ''),
     492            'type'      => $this->logPriorityToString($priority),
     493            'file:line' => "$file : $line",
     494            'message'   => $message
     495        );
    480496
    481497        // FILE ACTION
    482498        if ($this->getParam('log_file_priority') && $priority <= $this->getParam('log_file_priority')) {
     499            $event_str = '[' . join('] [', $event) . ']';
    483500            error_log($event_str . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename'));
    484501        }
Note: See TracChangeset for help on using the changeset viewer.