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 base64
import contextlib import contextlib
import io
import itertools import itertools
import os import os
import posixpath import posixpath
@ -130,9 +131,29 @@ class ThreadedHTTPSServer(socketserver.ThreadingMixIn, HTTPSServer):
class RequestHandler(wsgiref.simple_server.WSGIRequestHandler): class RequestHandler(wsgiref.simple_server.WSGIRequestHandler):
"""HTTP requests handler.""" """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): def log_message(self, *args, **kwargs):
"""Disable inner logging management.""" """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: class Application:
"""WSGI application managing collections.""" """WSGI application managing collections."""

View File

@ -104,7 +104,11 @@ def run():
if not configuration_found: if not configuration_found:
logger.warning("Configuration file '%s' not found" % options.config) logger.warning("Configuration file '%s' not found" % options.config)
try:
serve(configuration, logger) serve(configuration, logger)
except Exception:
logger.exception("An exception occurred during server startup:")
exit(1)
def serve(configuration, logger): def serve(configuration, logger):
@ -175,6 +179,7 @@ def serve(configuration, logger):
server_class.max_connections = configuration.getint( server_class.max_connections = configuration.getint(
"server", "max_connections") "server", "max_connections")
RequestHandler.logger = logger
if not configuration.getboolean("server", "dns_lookup"): if not configuration.getboolean("server", "dns_lookup"):
RequestHandler.address_string = lambda self: self.client_address[0] RequestHandler.address_string = lambda self: self.client_address[0]