From 7dd8a53c8c6afbc4199ff9b7fb2d6c3068fd9d9a Mon Sep 17 00:00:00 2001 From: "y.umezaki" Date: Wed, 21 Oct 2015 22:48:39 +0900 Subject: [PATCH] Add accesslog params Fix KeyError Update access logger tests Update settings.rst docs --- docs/source/settings.rst | 7 ++++++- gunicorn/config.py | 7 ++++++- gunicorn/glogging.py | 5 +++++ tests/test_logger.py | 14 +++++++++++--- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/source/settings.rst b/docs/source/settings.rst index 3e7de997..c2407ebc 100644 --- a/docs/source/settings.rst +++ b/docs/source/settings.rst @@ -475,8 +475,13 @@ l ``'-'`` u currently ``'-'``, may be user name in future releases t date of the request r status line (e.g. ``GET / HTTP/1.1``) +m request method +U URL path without query string +q query string +H protocol s status -b response length or ``'-'`` +B response length +b response length or ``'-'`` (CLF format) f referer a user agent T request time in seconds diff --git a/gunicorn/config.py b/gunicorn/config.py index a1aa4561..039a1800 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -1052,8 +1052,13 @@ class AccessLogFormat(Setting): u currently ``'-'``, may be user name in future releases t date of the request r status line (e.g. ``GET / HTTP/1.1``) + m request method + U URL path without query string + q query string + H protocol s status - b response length or ``'-'`` + B response length + b response length or ``'-'`` (CLF format) f referer a user agent T request time in seconds diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index e0819284..225b8863 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -245,7 +245,12 @@ class Logger(object): 'r': "%s %s %s" % (environ['REQUEST_METHOD'], environ['RAW_URI'], environ["SERVER_PROTOCOL"]), 's': status, + 'm': environ.get('REQUEST_METHOD'), + '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, 'f': environ.get('HTTP_REFERER', '-'), 'a': environ.get('HTTP_USER_AGENT', '-'), 'T': request_time.seconds, diff --git a/tests/test_logger.py b/tests/test_logger.py index 0b6a453b..e719ce02 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -13,13 +13,20 @@ def test_atoms_defaults(): ) request = SimpleNamespace(headers=(('Accept', 'application/json'),)) environ = { - 'REQUEST_METHOD': 'GET', 'RAW_URI': 'http://my.uri', + 'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar', + 'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar', 'SERVER_PROTOCOL': 'HTTP/1.1', } logger = Logger(Config()) atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1)) assert isinstance(atoms, dict) - assert atoms['r'] == 'GET http://my.uri HTTP/1.1' + assert atoms['r'] == 'GET /my/path?foo=bar HTTP/1.1' + assert atoms['m'] == 'GET' + assert atoms['U'] == '/my/path' + assert atoms['q'] == 'foo=bar' + assert atoms['H'] == 'HTTP/1.1' + assert atoms['b'] == '1024' + assert atoms['B'] == 1024 assert atoms['{accept}i'] == 'application/json' assert atoms['{content-type}o'] == 'application/json' @@ -31,7 +38,8 @@ def test_get_username_from_basic_auth_header(): headers=(('Content-Type', 'text/plain'),), ) environ = { - 'REQUEST_METHOD': 'GET', 'RAW_URI': 'http://my.uri', + 'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar', + 'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar', 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_AUTHORIZATION': 'Basic YnJrMHY6', }