/** * Logs a message to the error log along with its trace. * The backtrace is automatically retrieved, even if the entry is a string, so NO backtrace * information should be provided in the message. However, if you need to save other context * data, as the URL, HTTP headers, or ENV variables, these should be part of the message. * * @param mixed $entry Can be of type string, ErrorException or Exception * @param int $priority One of the Cli_Service_Log class constants. * @param bool $addTrace Whether to add a trace to log message. */ public static function log($entry, $priority = self::ERR, $addTrace = true) { if (!self::$_logger) { self::$_logger = Zend_Registry::get('errorLog'); } $logData = array('message' => '', 'priority' => $priority); if ($entry instanceof ErrorException) { $severity = $entry->getSeverity(); switch ($severity) { case E_NOTICE: $logData['priority'] = self::NOTICE; break; case E_WARNING: $logData['priority'] = self::WARN; break; case E_ERROR: default: $logData['priority'] = self::ERR; break; } if ($addTrace) { $logData['message'] = ''; $traceString = str_replace("#", "\t#", $entry->getTraceAsString()); $logData['message'] .= $entry->getMessage() . PHP_EOL . $traceString . PHP_EOL; } else { $logData['message'] = $entry->getMessage(); } } elseif ($entry instanceof Exception) { // add a tab beofre each new line of the trace string $logData['priority'] = $entry->getCode(); if ($addTrace) { $logData['message'] = ''; $traceString = str_replace("#", "\t#", $entry->getTraceAsString()); $logData['message'] .= $entry->getMessage() . PHP_EOL . $traceString . PHP_EOL; } else { $logData['message'] = $entry->getMessage(); } } elseif (is_string($entry)) { if ($addTrace) { $rawBacktrace = debug_backtrace(); $formattedBacktrace = self::_getFormattedBacktrace($rawBacktrace); $logData['message'] = $entry . PHP_EOL . $formattedBacktrace; } else { $logData['message'] = $entry; } } else { throw new BadMethodCallException('Logging service called with unknown entry type: ' . gettype($entry)); } if ($logData['priority'] >= self::EMERG && $logData['priority'] <= self::DEBUG) { self::$_logger->log($logData['message'], $logData['priority']); } else { self::$_logger->err($logData['message']); } if (ini_get('display_errors')) { echo $logData['message'] . PHP_EOL; } }
/** * Application error handler * * @param int $errno * @param string $errstr * @param mixed $errfile * @param int $errline * @return boolean */ public static function phpErrorsAction($errno, $errstr, $errfile, $errline) { Utility_Service_Log::log(new ErrorException($errstr, $errno, 0, $errfile, $errline)); // skip the execution of the default php error handler return true; }