mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Merge pull request #1193 from urbaniak/gaiohttp-logging
fix access logging in gaiohttp worker
This commit is contained in:
commit
d6a47e8aa2
@ -237,7 +237,9 @@ class Logger(object):
|
|||||||
def atoms(self, resp, req, environ, request_time):
|
def atoms(self, resp, req, environ, request_time):
|
||||||
""" Gets atoms for log formating.
|
""" 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 = {
|
atoms = {
|
||||||
'h': environ.get('REMOTE_ADDR', '-'),
|
'h': environ.get('REMOTE_ADDR', '-'),
|
||||||
'l': '-',
|
'l': '-',
|
||||||
@ -250,8 +252,8 @@ class Logger(object):
|
|||||||
'U': environ.get('PATH_INFO'),
|
'U': environ.get('PATH_INFO'),
|
||||||
'q': environ.get('QUERY_STRING'),
|
'q': environ.get('QUERY_STRING'),
|
||||||
'H': environ.get('SERVER_PROTOCOL'),
|
'H': environ.get('SERVER_PROTOCOL'),
|
||||||
'b': resp.sent and str(resp.sent) or '-',
|
'b': getattr(resp, 'sent', None) and str(resp.sent) or '-',
|
||||||
'B': resp.sent,
|
'B': getattr(resp, 'sent', None),
|
||||||
'f': environ.get('HTTP_REFERER', '-'),
|
'f': environ.get('HTTP_REFERER', '-'),
|
||||||
'a': environ.get('HTTP_USER_AGENT', '-'),
|
'a': environ.get('HTTP_USER_AGENT', '-'),
|
||||||
'T': request_time.seconds,
|
'T': request_time.seconds,
|
||||||
@ -266,10 +268,17 @@ class Logger(object):
|
|||||||
else:
|
else:
|
||||||
req_headers = req
|
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]))
|
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
|
# 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
|
return atoms
|
||||||
|
|
||||||
|
|||||||
@ -93,9 +93,12 @@ class Statsd(Logger):
|
|||||||
"""
|
"""
|
||||||
Logger.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
|
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.histogram("gunicorn.request.duration", duration_in_ms)
|
||||||
self.increment("gunicorn.requests", 1)
|
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
|
# statsD methods
|
||||||
# you can use those directly if you want
|
# you can use those directly if you want
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
# See the NOTICE for more information.
|
# See the NOTICE for more information.
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import datetime
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -15,7 +16,12 @@ except ImportError:
|
|||||||
|
|
||||||
import gunicorn.workers.base as base
|
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):
|
class AiohttpWorker(base.Worker):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user