Changeset 390 for trunk/lib/App.inc.php
- Timestamp:
- Nov 18, 2011 4:01:54 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/App.inc.php
r380 r390 62 62 // Public name and email address for this application. 63 63 'site_name' => null, 64 'site_email' => null,64 'site_email' => '', // Set to no-reply@HTTP_HOST if not set here. 65 65 'site_url' => '', // URL automatically determined by _SERVER['HTTP_HOST'] if not set here. 66 66 'images_path' => '', // Location for codebase-generated interface widgets (ex: "/admin/i"). … … 126 126 'log_filename' => 'app_log', 127 127 128 // Don't email or smsduplicate messages that happen more often than this value (in seconds).129 'log_multiple_timeout' => 3 00,128 // Don't email or SMS duplicate messages that happen more often than this value (in seconds). 129 'log_multiple_timeout' => 3600, // Hourly 130 130 131 131 // Logging priority can be any of the following, or false to deactivate: … … 143 143 'log_screen_priority' => false, 144 144 145 // Email address to receive log event emails. 145 // Email address to receive log event emails. Use multiple addresses by separating them with commas. 146 146 'log_to_email_address' => null, 147 147 … … 357 357 $this->setParam(array('site_url' => sprintf('%s://%s', ('on' == getenv('HTTPS') ? 'https' : 'http'), getenv('HTTP_HOST')))); 358 358 } 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 } 359 364 360 365 // A key for calculating simple cryptographic signatures. … … 525 530 /** 526 531 * 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). 528 534 * 529 535 * @access public 530 536 * @param string $message The text description of the message. 531 537 * @param int $priority The type of message priority (in descending order): 532 * LOG_EMERG system is unusable533 * LOG_ALERT action must be taken immediately534 * LOG_CRIT critical conditions535 * LOG_ERR error conditions536 * LOG_WARNING warning conditions537 * LOG_NOTICE normal, but significant, condition538 * LOG_INFO informational message539 * LOG_DEBUG debug-level message538 * 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 540 546 * @param string $file The file where the log event occurs. 541 547 * @param string $line The line of the file where the log event occurs. … … 556 562 // We must use trigger_error to report this problem rather than calling $app->logMsg, which might lead to an infinite loop. 557 563 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; 558 573 } 559 574 … … 578 593 } 579 594 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 } 594 618 } 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 } 604 622 605 623 // Data to be stored for a log event. … … 615 633 616 634 // 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')) { 618 636 $event_str = '[' . join('] [', $event) . ']'; 619 637 error_log(mb_substr($event_str, 0, 1024) . "\n", 3, $this->getParam('log_directory') . '/' . $this->getParam('log_filename')); 620 638 } 621 639 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); 642 657 } 643 658 644 659 // 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')) { 646 661 echo "[{$event['type']}] [{$event['message']}]\n"; 647 662 } … … 649 664 // Restore original locale. 650 665 setlocale(LC_TIME, $locale); 666 667 return true; 651 668 } 652 669
Note: See TracChangeset
for help on using the changeset viewer.