Log exceptions (Fixes #447)

Exceptions were just written to stderr but not into logs.
This commit is contained in:
Unrud
2016-08-25 04:33:14 +02:00
parent c091399f5e
commit 3b71ab960e
2 changed files with 27 additions and 1 deletions

View File

@ -28,6 +28,7 @@ should have been included in this package.
import base64
import contextlib
import io
import itertools
import os
import posixpath
@ -130,9 +131,29 @@ class ThreadedHTTPSServer(socketserver.ThreadingMixIn, HTTPSServer):
class RequestHandler(wsgiref.simple_server.WSGIRequestHandler):
"""HTTP requests handler."""
# These class attributes must be set before creating instance
logger = None
def __init__(self, *args, **kwargs):
# Store exception for logging
self.error_stream = io.StringIO()
super().__init__(*args, **kwargs)
def get_stderr(self):
return self.error_stream
def log_message(self, *args, **kwargs):
"""Disable inner logging management."""
def handle(self):
super().handle()
# Log exception
error = self.error_stream.getvalue().strip("\n")
if error:
self.logger.error("An exception occurred during request:\n" +
error)
class Application:
"""WSGI application managing collections."""