/** * Logs message or exception to file (if not disabled) and sends email notification (if enabled). * @param string|Exception * @param int one of constant Debugger::INFO, WARNING, ERROR (sends email), CRITICAL (sends email) * @return void */ public static function log($message, $priority = self::INFO) { if (self::$logDirectory === FALSE) { return; } elseif (!self::$logDirectory) { throw new Nette\InvalidStateException('Logging directory is not specified in Nette\\Diagnostics\\Debugger::$logDirectory.'); } if ($message instanceof \Exception) { $exception = $message; $message = "PHP Fatal error: " . ($message instanceof Nette\FatalErrorException ? $exception->getMessage() : "Uncaught exception " . get_class($exception) . " with message '" . $exception->getMessage() . "'") . " in " . $exception->getFile() . ":" . $exception->getLine(); $hash = md5($exception); $exceptionFilename = "exception " . @date('Y-m-d H-i-s') . " {$hash}.html"; foreach (new \DirectoryIterator(self::$logDirectory) as $entry) { if (strpos($entry, $hash)) { $exceptionFilename = NULL; break; } } } self::$logger->log(array(@date('[Y-m-d H-i-s]'), $message, self::$source ? ' @ ' . self::$source : NULL, !empty($exceptionFilename) ? ' @@ ' . $exceptionFilename : NULL), $priority); if (!empty($exceptionFilename) && ($logHandle = @fopen(self::$logDirectory . '/' . $exceptionFilename, 'w'))) { ob_start(); // double buffer prevents sending HTTP headers in some PHP ob_start(function ($buffer) use($logHandle) { fwrite($logHandle, $buffer); }, 1); self::$blueScreen->render($exception); ob_end_flush(); ob_end_clean(); fclose($logHandle); } }
/** * Logs message or exception to file (if not disabled) and sends email notification (if enabled). * @param string|Exception * @param int one of constant Debugger::INFO, WARNING, ERROR (sends email), CRITICAL (sends email) * @return string logged error filename */ public static function log($message, $priority = self::INFO) { if (self::$logDirectory === FALSE) { return; } elseif (!self::$logDirectory) { throw new Nette\InvalidStateException('Logging directory is not specified in Nette\\Diagnostics\\Debugger::$logDirectory.'); } if ($message instanceof \Exception) { $exception = $message; $message = ($message instanceof Nette\FatalErrorException ? 'Fatal error: ' . $exception->getMessage() : get_class($exception) . ": " . $exception->getMessage()) . " in " . $exception->getFile() . ":" . $exception->getLine(); $hash = md5($exception); $exceptionFilename = "exception-" . @date('Y-m-d-H-i-s') . "-{$hash}.html"; foreach (new \DirectoryIterator(self::$logDirectory) as $entry) { if (strpos($entry, $hash)) { $exceptionFilename = $entry; $saved = TRUE; break; } } } elseif (!is_string($message)) { $message = Helpers::textDump($message); } self::$logger->log(array(@date('[Y-m-d H-i-s]'), trim($message), self::$source ? ' @ ' . self::$source : NULL, !empty($exceptionFilename) ? ' @@ ' . $exceptionFilename : NULL), $priority); if (!empty($exceptionFilename)) { $exceptionFilename = self::$logDirectory . '/' . $exceptionFilename; if (empty($saved) && ($logHandle = @fopen($exceptionFilename, 'w'))) { ob_start(); // double buffer prevents sending HTTP headers in some PHP ob_start(function ($buffer) use($logHandle) { fwrite($logHandle, $buffer); }, 4096); self::$blueScreen->render($exception); ob_end_flush(); ob_end_clean(); fclose($logHandle); } return strtr($exceptionFilename, '\\/', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR); } }