Merge branch 'Unrud-logging'
This commit is contained in:
commit
0e646cdae4
2
logging
2
logging
@ -39,7 +39,7 @@ handlers = console,file
|
|||||||
# Console handler
|
# Console handler
|
||||||
class = StreamHandler
|
class = StreamHandler
|
||||||
level = INFO
|
level = INFO
|
||||||
args = (sys.stdout,)
|
args = (sys.stderr,)
|
||||||
formatter = simple
|
formatter = simple
|
||||||
|
|
||||||
[handler_file]
|
[handler_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
|
||||||
@ -131,6 +132,18 @@ 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."""
|
||||||
|
|
||||||
@ -140,6 +153,14 @@ class RequestHandler(wsgiref.simple_server.WSGIRequestHandler):
|
|||||||
env["PATH_INFO"] = urllib.parse.unquote(self.path.split("?", 1)[0])
|
env["PATH_INFO"] = urllib.parse.unquote(self.path.split("?", 1)[0])
|
||||||
return env
|
return env
|
||||||
|
|
||||||
|
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%s" % error)
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
"""WSGI application managing collections."""
|
"""WSGI application managing collections."""
|
||||||
|
@ -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]
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@ import signal
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def configure_from_file(filename, debug, logger):
|
def configure_from_file(logger, filename, debug):
|
||||||
logging.config.fileConfig(filename)
|
logging.config.fileConfig(filename, disable_existing_loggers=False)
|
||||||
if debug:
|
if debug:
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
for handler in logger.handlers:
|
for handler in logger.handlers:
|
||||||
@ -46,18 +46,16 @@ def start(name="radicale", filename=None, debug=False):
|
|||||||
configure_from_file(logger, filename, debug)
|
configure_from_file(logger, filename, debug)
|
||||||
# Reload config on SIGHUP (UNIX only)
|
# Reload config on SIGHUP (UNIX only)
|
||||||
if hasattr(signal, "SIGHUP"):
|
if hasattr(signal, "SIGHUP"):
|
||||||
def handler_generator(logger, filename, debug):
|
|
||||||
def handler(signum, frame):
|
def handler(signum, frame):
|
||||||
configure_from_file(logger, filename, debug)
|
configure_from_file(logger, filename, debug)
|
||||||
handler = handler_generator(logger, filename, debug)
|
|
||||||
signal.signal(signal.SIGHUP, handler)
|
signal.signal(signal.SIGHUP, handler)
|
||||||
else:
|
else:
|
||||||
# Default configuration, standard output
|
# Default configuration, standard output
|
||||||
if filename:
|
if filename:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Logging configuration file '%s' not found, using stdout." %
|
"Logging configuration file '%s' not found, using stderr." %
|
||||||
filename)
|
filename)
|
||||||
handler = logging.StreamHandler(sys.stdout)
|
handler = logging.StreamHandler(sys.stderr)
|
||||||
handler.setFormatter(logging.Formatter("%(message)s"))
|
handler.setFormatter(logging.Formatter("%(message)s"))
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
if debug:
|
if debug:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user