diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index 2e3fd527c..671a589ad 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -237,7 +237,9 @@ def log(self, lvl, msg, *args, **kwargs): def atoms(self, resp, req, environ, request_time): """ Gets atoms for log formating. """ - status = resp.status.split(None, 1)[0] + status = resp.status + if isinstance(status, str): + status = status.split(None, 1)[0] atoms = { 'h': environ.get('REMOTE_ADDR', '-'), 'l': '-', @@ -250,8 +252,8 @@ def atoms(self, resp, req, environ, request_time): 'U': environ.get('PATH_INFO'), 'q': environ.get('QUERY_STRING'), 'H': environ.get('SERVER_PROTOCOL'), - 'b': resp.sent and str(resp.sent) or '-', - 'B': resp.sent, + 'b': getattr(resp, 'sent', None) and str(resp.sent) or '-', + 'B': getattr(resp, 'sent', None), 'f': environ.get('HTTP_REFERER', '-'), 'a': environ.get('HTTP_USER_AGENT', '-'), 'T': request_time.seconds, @@ -266,10 +268,17 @@ def atoms(self, resp, req, environ, request_time): else: req_headers = req + if hasattr(req_headers, "items"): + req_headers = req_headers.items() + atoms.update(dict([("{%s}i" % k.lower(), v) for k, v in req_headers])) + resp_headers = resp.headers + if hasattr(resp_headers, "items"): + resp_headers = resp_headers.items() + # add response headers - atoms.update(dict([("{%s}o" % k.lower(), v) for k, v in resp.headers])) + atoms.update(dict([("{%s}o" % k.lower(), v) for k, v in resp_headers])) return atoms diff --git a/gunicorn/instrument/statsd.py b/gunicorn/instrument/statsd.py index 58bccdf82..08cdf9356 100644 --- a/gunicorn/instrument/statsd.py +++ b/gunicorn/instrument/statsd.py @@ -93,9 +93,12 @@ def access(self, resp, req, environ, request_time): """ Logger.access(self, resp, req, environ, request_time) duration_in_ms = request_time.seconds * 1000 + float(request_time.microseconds) / 10 ** 3 + status = resp.status + if isinstance(status, str): + status = int(status.split(None, 1)[0]) self.histogram("gunicorn.request.duration", duration_in_ms) self.increment("gunicorn.requests", 1) - self.increment("gunicorn.request.status.%d" % int(resp.status.split()[0]), 1) + self.increment("gunicorn.request.status.%d" % status, 1) # statsD methods # you can use those directly if you want diff --git a/gunicorn/workers/_gaiohttp.py b/gunicorn/workers/_gaiohttp.py index b3ebc0f49..cdce4be75 100644 --- a/gunicorn/workers/_gaiohttp.py +++ b/gunicorn/workers/_gaiohttp.py @@ -4,6 +4,7 @@ # See the NOTICE for more information. import asyncio +import datetime import functools import logging import os @@ -15,7 +16,12 @@ import gunicorn.workers.base as base -from aiohttp.wsgi import WSGIServerHttpProtocol +from aiohttp.wsgi import WSGIServerHttpProtocol as OldWSGIServerHttpProtocol + + +class WSGIServerHttpProtocol(OldWSGIServerHttpProtocol): + def log_access(self, request, environ, response, time): + self.logger.access(response, request, environ, datetime.timedelta(0, 0, time)) class AiohttpWorker(base.Worker):