diff --git a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py index 591e82d..ddadcbd 100644 --- a/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py +++ b/gunicorn-logging-extension/src/gunicorn_logging_extension/__init__.py @@ -10,7 +10,7 @@ from logging.config import dictConfig, fileConfig from gunicorn.glogging import CONFIG_DEFAULTS from gunicorn.glogging import Logger as gLogger -__version__ = "0.0.2" +__version__ = "0.0.3" class Logger(gLogger): @@ -50,16 +50,30 @@ 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) - def atoms(self, resp, req, environ, request_time): - atoms = super().atoms(resp, req, environ, request_time) - atoms["extra"] = dict( + def access(self, resp, req, environ, request_time): + """See http://httpd.apache.org/docs/2.0/logs.html#combined + for format details + """ + + if not ( + self.cfg.accesslog + or self.cfg.logconfig + or self.cfg.logconfig_dict + or self.cfg.logconfig_json + or (self.cfg.syslog and not self.cfg.disable_redirect_access_to_syslog) + ): + return + + # wrap atoms: + # - make sure atoms will be test case insensitively + # - if atom doesn't exist replace it by '-' + 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"), query=environ.get("QUERY_STRING"), @@ -67,11 +81,15 @@ class Logger(gLogger): time="%d.%06d" % (request_time.seconds, request_time.microseconds), ) if environ.get("REMOTE_ADDR", False): - atoms["extra"]["remote"] = environ.get("REMOTE_ADDR") + extra["remote"] = environ.get("REMOTE_ADDR") if getattr(resp, "sent", False): - atoms["extra"]["length"] = getattr(resp, "sent") + extra["length"] = getattr(resp, "sent") if environ.get("HTTP_REFERER", False): - atoms["extra"]["referer"] = environ.get("HTTP_REFERER") + extra["referer"] = environ.get("HTTP_REFERER") if environ.get("HTTP_USER_AGENT", False): - atoms["extra"]["user_agent"] = environ.get("HTTP_USER_AGENT") - return atoms + extra["user_agent"] = environ.get("HTTP_USER_AGENT") + + try: + self.access_log.info(self.cfg.access_log_format, safe_atoms, extra=extra) + except Exception: + self.error(traceback.format_exc())