/** * exception handler; allows to remove paths from error messages and show * optional stack trace if CAT_Helper_DB::$trace is true **/ public static function exceptionHandler($exception) { if (CAT_Helper_DB::$exc_trace === true) { $traceline = "#%s %s(%s): %s(%s)"; $msg = "Uncaught exception '%s' with message '%s'<br />" . "<div style=\"font-size:smaller;width:80%%;margin:5px auto;text-align:left;\">" . "in %s:%s<br />Stack trace:<br />%s<br />" . "thrown in %s on line %s</div>"; $trace = $exception->getTrace(); foreach ($trace as $key => $stackPoint) { $trace[$key]['args'] = array_map('gettype', $trace[$key]['args']); } // build tracelines $result = array(); foreach ($trace as $key => $stackPoint) { $result[] = sprintf($traceline, $key, isset($stackPoint['file']) ? $stackPoint['file'] : '-', isset($stackPoint['line']) ? $stackPoint['line'] : '-', $stackPoint['function'], implode(', ', $stackPoint['args'])); } // trace always ends with {main} $result[] = '#' . ++$key . ' {main}'; // write tracelines into main template $msg = sprintf($msg, get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine(), implode("<br />", $result), $exception->getFile(), $exception->getLine()); } else { // template $msg = "[DB Exception] %s<br />"; // filter message $message = $exception->getMessage(); preg_match('~SQLSTATE\\[[^\\]].+?\\]\\s+\\[[^\\]].+?\\]\\s+(.*)~i', $message, $match); $msg = sprintf($msg, isset($match[1]) ? $match[1] : $message); } try { $logger = CAT_Helper_KLogger::instance(CAT_PATH . '/temp/logs', 2); $logger->logFatal(sprintf('Exception with message [%s] emitted in [%s] line [%s]', $exception->getMessage(), $exception->getFile(), $exception->getLine())); $logger->logFatal($msg); } catch (Exception $e) { } // log or echo as you please CAT_Object::printFatalError($msg); }
/** * global shutdown handler; allows to log errors that caused a shutdown **/ public static function shutdownHandler() { $lasterror = error_get_last(); switch ($lasterror['type']) { case E_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: case E_USER_ERROR: case E_RECOVERABLE_ERROR: case E_CORE_WARNING: case E_COMPILE_WARNING: case E_PARSE: $error = "[SHUTDOWN] lvl:" . $lasterror['type'] . " | msg:" . $lasterror['message'] . " | file:" . $lasterror['file'] . " | ln:" . $lasterror['line']; $logger = CAT_Helper_KLogger::instance(CAT_PATH . '/temp/logs', 2); $logger->logFatal($error); } }
/** * Accessor to KLogger class; this makes using the class significant faster! * * @access public * @return object * **/ public function log() { // 8 = OFF if ($this->debugLevel < 8) { if (!is_object($this->logObj)) { if (!CAT_Registry::exists('CAT_PATH', false)) { CAT_Registry::define('CAT_PATH', dirname(__FILE__) . '/../..', 1); } $debug_dir = CAT_PATH . '/temp/logs' . ($this->debugLevel == 7 ? '/debug_' . get_class($this) : ''); if (get_class($this) != 'CAT_Helper_Directory') { $debug_dir = CAT_Helper_Directory::sanitizePath($debug_dir); } if (!file_exists($debug_dir)) { if (get_class($this) != 'CAT_Helper_Directory') { CAT_Helper_Directory::createDirectory($debug_dir, 0777); } else { mkdir($debug_dir, 0777); } } $this->logObj = CAT_Helper_KLogger::instance($debug_dir, $this->debugLevel); } return $this->logObj; } return $this; }