diff --git a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py index 1ac667d..591e82d 100644 --- a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py +++ b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py @@ -1,13 +1,16 @@ # SPDX-FileCopyrightText: 2024-present Sebastian Tobie # # SPDX-License-Identifier: MIT -from gunicorn.glogging import Logger as gLogger -from logging import Formatter -from logging.config import dictConfig -from logging.config import fileConfig +import json +import logging import os.path +import threading +from logging.config import dictConfig, fileConfig -__version__ = "0.0.1" +from gunicorn.glogging import CONFIG_DEFAULTS +from gunicorn.glogging import Logger as gLogger + +__version__ = "0.0.2" class Logger(gLogger): @@ -34,14 +37,22 @@ class Logger(gLogger): config_json = json.load(open(cfg.logconfig_json)) config.update(config_json) dictConfig(config) - except (json.JSONDecodeError, AttributeError, ImportError, ValueError, TypeError) as exc: + except ( + json.JSONDecodeError, + AttributeError, + ImportError, + ValueError, + TypeError, + ) as exc: raise RuntimeError(str(exc)) elif cfg.logconfig: if os.path.exists(cfg.logconfig): defaults = CONFIG_DEFAULTS.copy() - defaults['__file__'] = cfg.logconfig - defaults['here'] = os.path.dirname(cfg.logconfig) - fileConfig(cfg.logconfig, defaults=defaults, disable_existing_loggers=False) + defaults["__file__"] = cfg.logconfig + defaults["here"] = os.path.dirname(cfg.logconfig) + fileConfig( + cfg.logconfig, defaults=defaults, disable_existing_loggers=False + ) else: msg = "Error: log config '%s' not found" raise RuntimeError(msg % cfg.logconfig) @@ -49,18 +60,18 @@ class Logger(gLogger): def atoms(self, resp, req, environ, request_time): atoms = super().atoms(resp, req, environ, request_time) atoms["extra"] = dict( - method=environ.get('REQUEST_METHOD'), - path=environ.get('PATH_INFO'), - query=environ.get('QUERY_STRING'), - proto=environ.get('SERVER_PROTOCOL'), + method=environ.get("REQUEST_METHOD"), + path=environ.get("PATH_INFO"), + query=environ.get("QUERY_STRING"), + proto=environ.get("SERVER_PROTOCOL"), time="%d.%06d" % (request_time.seconds, request_time.microseconds), ) - if environ.get('REMOTE_ADDR', False): - atoms["extra"]["remote"] = environ.get('REMOTE_ADDR') - if getattr(resp, 'sent', False): - atoms["extra"]["length"] = getattr(resp, 'sent') - if environ.get('HTTP_REFERER', False): - atoms["extra"]["referer"] = environ.get('HTTP_REFERER') - if environ.get('HTTP_USER_AGENT', False): - atoms["extra"]["user_agent"] = environ.get('HTTP_USER_AGENT') + if environ.get("REMOTE_ADDR", False): + atoms["extra"]["remote"] = environ.get("REMOTE_ADDR") + if getattr(resp, "sent", False): + atoms["extra"]["length"] = getattr(resp, "sent") + if environ.get("HTTP_REFERER", False): + atoms["extra"]["referer"] = environ.get("HTTP_REFERER") + if environ.get("HTTP_USER_AGENT", False): + atoms["extra"]["user_agent"] = environ.get("HTTP_USER_AGENT") return atoms