From d1164234587ae969b71719d7724451e276552471 Mon Sep 17 00:00:00 2001 From: Peter Bieringer Date: Sat, 17 Sep 2016 15:35:43 +0200 Subject: [PATCH 1/4] improved request logging --- radicale/__init__.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index c5266e85..5ef86f02 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -40,6 +40,7 @@ import threading import urllib import wsgiref.simple_server import zlib +import datetime from http import client from urllib.parse import unquote, urlparse @@ -295,15 +296,28 @@ class Application: headers[key] = self.configuration.get("headers", key) # Start response + time_end = datetime.datetime.now() status = "%i %s" % ( status, client.responses.get(status, "Unknown")) - self.logger.debug("Answer status: %s", status) + self.logger.info("%s answer status for %s in %s sec: %s", environ["REQUEST_METHOD"], environ["PATH_INFO"], (time_end - time_begin).total_seconds(), status) start_response(status, list(headers.items())) # Return response content return [answer] if answer else [] - self.logger.info("%s request at %s received", - environ["REQUEST_METHOD"], environ["PATH_INFO"]) + remote_host = "unkown" + if "REMOTE_HOST" in environ: + if environ["REMOTE_HOST"]: + remote_host = environ["REMOTE_HOST"] + if "HTTP_X_FORWARDED_FOR" in environ: + if environ["HTTP_X_FORWARDED_FOR"]: + remote_host = environ["HTTP_X_FORWARDED_FOR"] + remote_useragent = "[-no-user-agent-provided-]" + if "HTTP_USER_AGENT" in environ: + if environ["HTTP_USER_AGENT"]: + remote_useragent = environ["HTTP_USER_AGENT"] + time_begin = datetime.datetime.now() + self.logger.info("%s request for %s received from %s using \"%s\"", + environ["REQUEST_METHOD"], environ["PATH_INFO"], remote_host, remote_useragent) headers = pprint.pformat(self.headers_log(environ)) self.logger.debug("Request headers:\n%s", headers) From 87061df68f2231f4588d129bf07c36ec60b56915 Mon Sep 17 00:00:00 2001 From: Peter Bieringer Date: Mon, 19 Sep 2016 19:59:47 +0200 Subject: [PATCH 2/4] add conditional logging of given depth --- radicale/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 5ef86f02..798c5128 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -304,7 +304,7 @@ class Application: # Return response content return [answer] if answer else [] - remote_host = "unkown" + remote_host = "UNKNOWN" if "REMOTE_HOST" in environ: if environ["REMOTE_HOST"]: remote_host = environ["REMOTE_HOST"] @@ -315,9 +315,12 @@ class Application: if "HTTP_USER_AGENT" in environ: if environ["HTTP_USER_AGENT"]: remote_useragent = environ["HTTP_USER_AGENT"] + depthinfo = "" + if environ["HTTP_DEPTH"]: + depthinfo = " with depth " + environ["HTTP_DEPTH"] time_begin = datetime.datetime.now() self.logger.info("%s request for %s received from %s using \"%s\"", - environ["REQUEST_METHOD"], environ["PATH_INFO"], remote_host, remote_useragent) + environ["REQUEST_METHOD"], environ["PATH_INFO"] + depthinfo, remote_host, remote_useragent) headers = pprint.pformat(self.headers_log(environ)) self.logger.debug("Request headers:\n%s", headers) From f52fa73cab6d8d225e588e08925be0edcc9abd8f Mon Sep 17 00:00:00 2001 From: Peter Bieringer Date: Mon, 19 Sep 2016 20:04:11 +0200 Subject: [PATCH 3/4] check env first before using HTTP_DEPTH --- radicale/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 798c5128..87ec4c82 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -316,8 +316,9 @@ class Application: if environ["HTTP_USER_AGENT"]: remote_useragent = environ["HTTP_USER_AGENT"] depthinfo = "" - if environ["HTTP_DEPTH"]: - depthinfo = " with depth " + environ["HTTP_DEPTH"] + if "HTTP_DEPTH" in environ: + if environ["HTTP_DEPTH"]: + depthinfo = " with depth " + environ["HTTP_DEPTH"] time_begin = datetime.datetime.now() self.logger.info("%s request for %s received from %s using \"%s\"", environ["REQUEST_METHOD"], environ["PATH_INFO"] + depthinfo, remote_host, remote_useragent) From c578470fc32b46c98e9d6210cdb73d14c270c7de Mon Sep 17 00:00:00 2001 From: Peter Bieringer Date: Mon, 19 Sep 2016 20:11:52 +0200 Subject: [PATCH 4/4] log depth also on response log line --- radicale/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 87ec4c82..46f7ec84 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -299,7 +299,7 @@ class Application: time_end = datetime.datetime.now() status = "%i %s" % ( status, client.responses.get(status, "Unknown")) - self.logger.info("%s answer status for %s in %s sec: %s", environ["REQUEST_METHOD"], environ["PATH_INFO"], (time_end - time_begin).total_seconds(), status) + self.logger.info("%s answer status for %s in %s sec: %s", environ["REQUEST_METHOD"], environ["PATH_INFO"] + depthinfo, (time_end - time_begin).total_seconds(), status) start_response(status, list(headers.items())) # Return response content return [answer] if answer else []