From e46094b73ac5e75da27fe38343d71b9c66fa10d8 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sun, 10 Mar 2024 01:06:01 +0100 Subject: [PATCH] added redirect info for gunicorn --- .../src/gunicorn_logging_extension/__init__.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py index dce9926..ef8a4e6 100644 --- a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py +++ b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py @@ -35,6 +35,8 @@ CONFIG_DEFAULTS = { }, } +REDIRECT_CODES = (301, 302, 303, 307, 308) + class Logger(gLogger): access_log_format = "Access(%(s)s) %(r)s" @@ -75,9 +77,7 @@ class Logger(gLogger): defaults = CONFIG_DEFAULTS.copy() defaults["__file__"] = cfg.logconfig defaults["here"] = os.path.dirname(cfg.logconfig) - fileConfig( - cfg.logconfig, defaults=defaults, disable_existing_loggers=False - ) + fileConfig(cfg.logconfig, defaults=defaults, disable_existing_loggers=False) else: msg = "Error: log config '%s' not found" raise RuntimeError(msg % cfg.logconfig) @@ -91,9 +91,7 @@ class Logger(gLogger): level = self.access_log.warning elif resp.status_code >= 500: level = self.access_log.error - safe_atoms = self.atoms_wrapper_class( - self.atoms(resp, req, environ, request_time) - ) + safe_atoms = self.atoms_wrapper_class(self.atoms(resp, req, environ, request_time)) extra = dict( METHOD=environ.get("REQUEST_METHOD"), PATH=environ.get("PATH_INFO"), @@ -111,7 +109,11 @@ class Logger(gLogger): extra["REFERER"] = environ.get("HTTP_REFERER") if environ.get("HTTP_USER_AGENT", False): extra["USER_AGENT"] = environ.get("HTTP_USER_AGENT") - + if resp.status_code in REDIRECT_CODES: + for header, value in resp.headers: + if header == "Location": + extra["LOCATION"] = value + break try: level(self.access_log_format, safe_atoms, extra=extra) except Exception: