diff --git a/lib/SimpleSAML/Error/Exception.php b/lib/SimpleSAML/Error/Exception.php
index 0300b7e616e81389f23972d7d46f43e34b5a3de1..8820448e8504a1eae5af4d389fba7d080af3bee0 100644
--- a/lib/SimpleSAML/Error/Exception.php
+++ b/lib/SimpleSAML/Error/Exception.php
@@ -92,6 +92,96 @@ class SimpleSAML_Error_Exception extends Exception {
 	}
 
 
+	/**
+	 * Format this exception for logging.
+	 *
+	 * Create an array with lines for logging.
+	 *
+	 * @return array  Log lines which should be written out.
+	 */
+	public function format() {
+
+		$ret = array();
+
+		$e = $this;
+		do {
+			$err = get_class($e) . ': ' . $e->getMessage();
+			if ($e === $this) {
+				$ret[] = $err;
+			} else {
+				$ret[] = 'Caused by: ' . $err;
+			}
+
+			$ret[] = 'Backtrace:';
+
+			$depth = count($e->backtrace);
+			foreach ($e->backtrace as $i => $trace) {
+				$ret[] = ($depth - $i - 1) . ' ' . $trace;
+			}
+
+			$e = $e->cause;
+		} while ($e !== NULL);
+
+		return $ret;
+	}
+
+
+	/**
+	 * Print the exception to the log with log level error.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logError() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::error($line);
+		}
+	}
+
+
+	/**
+	 * Print the exception to the log with log level warning.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logWarning() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::warning($line);
+		}
+	}
+
+
+	/**
+	 * Print the exception to the log with log level info.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logInfo() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::debug($line);
+		}
+	}
+
+
+	/**
+	 * Print the exception to the log with log level debug.
+	 *
+	 * This function will write this exception to the log, including a full backtrace.
+	 */
+	public function logDebug() {
+
+		$lines = $this->format();
+		foreach ($lines as $line) {
+			SimpleSAML_Logger::debug($line);
+		}
+	}
+
+
 	/**
 	 * Function for serialization.
 	 *