/** * Builds the log file entry for the given log message. * * TODO: Use localized time in local log files * * @param string $logLevel Log level of the message to log * @param string $message The message to log * @param \Exception|null $exception Exception belonging to the log entry * @return string */ private function buildLogEntry(string $logLevel, string $message, Exception $exception = null) { // build base log entry $logEntry = sprintf('%s %s %s', DateUtil::getCurrentUtcDateTime()->format('Y-m-d H:i:s'), StringUtil::toUpperCase($logLevel), $message); // add exception information if present if ($exception !== null) { $logEntry .= StringUtil::CHAR_LINE_FEED . sprintf('Exception: %s in file %s on line %s', $exception->getMessage(), $exception->getFile(), $exception->getLine()) . StringUtil::CHAR_LINE_FEED . ExceptionUtil::getFullTrace($exception); } // return log entry return $logEntry; }
/** * Tests whether getTraceAsString() works as expected. * * @dataProvider dataProviderTestGetTraceAsString */ public function testGetTraceAsString($trace, $expectedResult) { $this->assertSame($expectedResult, ExceptionUtil::getTraceAsString($trace)); }
/** * Returns an appropriate PSR-3 log level for the given native PHP error severity. * * @param int $severity The native PHP severity * @throws \Ableron\Core\Exception\SystemException In case the given severity is unknown * @return string */ public function toPsr3LogLevel(int $severity) { switch (ExceptionUtil::getSeverityCategory($severity)) { case ExceptionUtil::SEVERITY_CATEGORY_ERROR: return LogLevel::ERROR; case ExceptionUtil::SEVERITY_CATEGORY_WARNING: return LogLevel::WARNING; case ExceptionUtil::SEVERITY_CATEGORY_INFO: return LogLevel::NOTICE; default: throw new SystemException('Unknown PHP error severity category for severity ' . $severity, 0, E_USER_ERROR, __FILE__, __LINE__); } }
/** * Handles uncaught exceptions. * * The cast to integer for the exception code is necessary, because there * are exceptions with a non-numeric exception code (e.g. PDOException has * something like "HY000". * * @param \Throwable $t The uncaught exception. Can be either {@link \Exception} or {@link \Error} as of PHP 7, thus needs to be \Throwable * @return void */ public static final function handleException(Throwable $t) { // convert given throwable into internal exception if necessary $exception = $t instanceof SystemException ? $t : new SystemException($t->getMessage(), (int) $t->getCode(), E_USER_ERROR, $t->getFile(), $t->getLine(), $t->getPrevious()); // log exception self::getLogManager()->logException('Uncaught exception', $exception); // stop script execution on fatal errors if (ExceptionUtil::severityIsError($exception->getSeverity())) { self::exitWithInternalServerError(); } }