Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make access log use local time with timezone #3860

Merged
merged 26 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions aiohttp/web_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import re
from time import timezone
from collections import namedtuple
from typing import Callable, Dict, Iterable, List, Tuple # noqa

Expand Down Expand Up @@ -149,9 +150,10 @@ def _format_a(request: BaseRequest,
def _format_t(request: BaseRequest,
response: StreamResponse,
time: float) -> str:
now = datetime.datetime.utcnow()
tz = datetime.timezone(datetime.timedelta(seconds=-timezone))
now = datetime.datetime.now(tz)
start_time = now - datetime.timedelta(seconds=time)
return start_time.strftime('[%d/%b/%Y:%H:%M:%S +0000]')
return start_time.strftime('[%d/%b/%Y:%H:%M:%S %z]')

@staticmethod
def _format_P(request: BaseRequest,
Expand Down
29 changes: 23 additions & 6 deletions tests/test_web_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ def test_access_logger_format() -> None:
Ref: https://stackoverflow.com/a/46102240/595220
""", # noqa: E501
)
def test_access_logger_atoms(mocker) -> None:
utcnow = datetime.datetime(1843, 1, 1, 0, 30)
def test_access_logger_time(mocker) -> None:
NewUserHa marked this conversation as resolved.
Show resolved Hide resolved
now = datetime.datetime(1843, 1, 1, 0, 30)
mock_datetime = mocker.patch("aiohttp.datetime.datetime")
mock_timezone = mocker.patch("aiohttp.timezone")
mock_datetime.now.return_value = now
mock_timezone.return_value = 28800
log_format = '%t'
mock_logger = mock.Mock()
access_logger = AccessLogger(mock_logger, log_format)
request = mock.Mock()
response = mock.Mock()
access_logger.log(request, response, 3.1415926)
assert not mock_logger.exception.called
expected = ('[01/Jan/1843:00:29:56 +0000]')
NewUserHa marked this conversation as resolved.
Show resolved Hide resolved
extra = {
'request_start_time': '[01/Jan/1843:00:29:56 +0000]',
NewUserHa marked this conversation as resolved.
Show resolved Hide resolved
}

mock_logger.info.assert_called_with(expected, extra=extra)


def test_access_logger_atoms_without_time(mocker) -> None:
mock_getpid = mocker.patch("os.getpid")
mock_datetime.utcnow.return_value = utcnow
mock_getpid.return_value = 42
log_format = '%a %t %P %r %s %b %T %Tf %D "%{H1}i" "%{H2}i"'
log_format = '%a %P %r %s %b %T %Tf %D "%{H1}i" "%{H2}i"'
mock_logger = mock.Mock()
access_logger = AccessLogger(mock_logger, log_format)
request = mock.Mock(headers={'H1': 'a', 'H2': 'b'},
Expand All @@ -59,13 +77,12 @@ def test_access_logger_atoms(mocker) -> None:
response = mock.Mock(headers={}, body_length=42, status=200)
access_logger.log(request, response, 3.1415926)
assert not mock_logger.exception.called
expected = ('127.0.0.2 [01/Jan/1843:00:29:56 +0000] <42> '
expected = ('127.0.0.2 <42> '
'GET /path HTTP/1.1 200 42 3 3.141593 3141593 "a" "b"')
extra = {
'first_request_line': 'GET /path HTTP/1.1',
'process_id': '<42>',
'remote_address': '127.0.0.2',
'request_start_time': '[01/Jan/1843:00:29:56 +0000]',
'request_time': 3,
'request_time_frac': '3.141593',
'request_time_micro': 3141593,
Expand Down