Пример #1
0
 /**
  * Custom error handler that logs to our own error log
  * 
  * @param int $errno
  * @param string $errstr
  * @param string $errfile
  * @param int $errline
  * @return boolean
  */
 public static function errorHandler($errno, $errstr, $errfile, $errline)
 {
     //prevent that the shutdown function will log this error again.
     if (self::$_lastReportedError == $errno . $errfile . $errline) {
         return;
     }
     self::$_lastReportedError = $errno . $errfile . $errline;
     //log only errors that are in error_reporting
     $error_reporting = ini_get('error_reporting');
     if (!($error_reporting & $errno)) {
         return;
     }
     $type = "Unknown error";
     switch ($errno) {
         case E_ERROR:
         case E_USER_ERROR:
             $type = 'Fatal error';
             break;
         case E_WARNING:
         case E_USER_WARNING:
             $type = 'Warning';
             break;
         case E_NOTICE:
         case E_USER_NOTICE:
             $type = 'Notice';
             break;
     }
     $errorMsg = "[" . @date("Ymd H:i:s") . "] PHP {$type}: {$errstr} in {$errfile} on line {$errline}";
     $user = isset(\GO::session()->values['username']) ? \GO::session()->values['username'] : '******';
     $agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'unknown';
     $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown';
     $errorMsg .= "\nUser: "******" Agent: " . $agent . " IP: " . $ip . "\n";
     if (isset($_SERVER['QUERY_STRING'])) {
         $errorMsg .= "Query: " . $_SERVER['QUERY_STRING'] . "\n";
     }
     $backtrace = debug_backtrace();
     array_shift($backtrace);
     //first item is this function which we don't have to see
     $errorMsg .= "Backtrace:\n";
     foreach ($backtrace as $o) {
         if (!isset($o['class'])) {
             $o['class'] = 'global';
         }
         if (!isset($o['function'])) {
             $o['function'] = 'global';
         }
         if (!isset($o['file'])) {
             $o['file'] = 'unknown';
         }
         if (!isset($o['line'])) {
             $o['line'] = 'unknown';
         }
         $errorMsg .= $o['class'] . '::' . $o['function'] . ' in file ' . $o['file'] . ' on line ' . $o['line'] . "\n";
     }
     $errorMsg .= "----------------";
     \GO::debug($errorMsg);
     \GO::logError($errorMsg);
     foreach (self::$_errorLogCallbacks as $callback) {
         call_user_func($callback, $errorMsg);
     }
     if (\GO::config()->debug) {
         echo nl2br($errorMsg);
     }
     /* Execute PHP internal error handler too */
     return false;
 }