From 7c1a1d176d4b5483ed008c303b5956d453a1ffb5 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Wed, 6 Mar 2024 21:33:44 +0100 Subject: [PATCH] improved the logging --- .../src/gunicorn_logging_extension/__init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py index 7cfc1a1..536450a 100644 --- a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py +++ b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py @@ -11,7 +11,7 @@ from gunicorn.glogging import CONFIG_DEFAULTS from gunicorn.glogging import Logger as gLogger import traceback -__version__ = "0.0.5" +__version__ = "0.0.6" CONFIG_DEFAULTS = { @@ -79,7 +79,11 @@ class Logger(gLogger): """See http://httpd.apache.org/docs/2.0/logs.html#combined for format details """ - + level = self.access_log.info + if resp.status_code >= 400: + level = self.access_log.warning + elif resp.status_code >= 500 + level = self.access_log.error # wrap atoms: # - make sure atoms will be test case insensitively # - if atom doesn't exist replace it by '-' @@ -90,6 +94,8 @@ class Logger(gLogger): QUERY=environ.get("QUERY_STRING"), PROTOCOL=environ.get("SERVER_PROTOCOL"), TIME="%d.%06d" % (request_time.seconds, request_time.microseconds), + STATUS_CODE=resp.status_code, + CODE_ARGS=None, ) if environ.get("REMOTE_ADDR", False): extra["REMOTE"] = environ.get("REMOTE_ADDR") @@ -101,6 +107,6 @@ class Logger(gLogger): extra["USER_AGENT"] = environ.get("HTTP_USER_AGENT") try: - self.access_log.info(self.cfg.access_log_format, safe_atoms, extra=extra) + level(self.cfg.access_log_format, safe_atoms, extra=extra) except Exception: self.error(traceback.format_exc())