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

Conversation

NewUserHa
Copy link
Contributor

@NewUserHa NewUserHa commented Jun 21, 2019

What do these changes do?

change access log use localtime and timezone instead all uctime

Are there changes in behavior for the user?

show localtime instead utctime if turn on access log

Related issue number

Fixes #3853

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@codecov-io
Copy link

codecov-io commented Jun 21, 2019

Codecov Report

Merging #3860 into master will decrease coverage by 0.09%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #3860     +/-   ##
=========================================
- Coverage   97.96%   97.86%   -0.1%     
=========================================
  Files          43       43             
  Lines        8588     8590      +2     
  Branches     1373     1373             
=========================================
- Hits         8413     8407      -6     
- Misses         71       78      +7     
- Partials      104      105      +1
Impacted Files Coverage Δ
aiohttp/web_log.py 97% <100%> (+0.06%) ⬆️
aiohttp/locks.py 92.85% <0%> (-7.15%) ⬇️
aiohttp/web_fileresponse.py 96.55% <0%> (-1.15%) ⬇️
aiohttp/streams.py 97.92% <0%> (-0.52%) ⬇️
aiohttp/helpers.py 96.96% <0%> (-0.51%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4273c7f...c5f66e6. Read the comment docs.

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test and changelog record would be awesome!

@NewUserHa
Copy link
Contributor Author

I read the test codes about web_log.py. but how to test this since there are expected and extras dicts in it?

aiohttp/web_log.py Outdated Show resolved Hide resolved
@webknjaz
Copy link
Member

@NewUserHa google pytest monkeypatch

@asvetlov
Copy link
Member

tests/test_web_log.py
Something like:

def test_logger_time() -> None:
    mock_logger = mock.Mock()
    access_logger = AccessLogger(mock_logger, "%t")
    access_logger.log(None, None, 0)
    mock_logger.info.assert_called_with("[1/2/3:12:13:14 +0200]", extra={'request_start_time': ...})

No need for monkey-patching here

@NewUserHa
Copy link
Contributor Author

NewUserHa commented Jun 21, 2019

I know. the problem is:
there's a test already like that:

def test_access_logger_atoms(mocker) -> None:
    utcnow = datetime.datetime(1843, 1, 1, 0, 30)
    mock_datetime = mocker.patch("aiohttp.datetime.datetime")
    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"'
    mock_logger = mock.Mock()
    access_logger = AccessLogger(mock_logger, log_format)
    request = mock.Mock(headers={'H1': 'a', 'H2': 'b'},
                        method="GET", path_qs="/path",
                        version=(1, 1),
                        remote="127.0.0.2")
    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> '
                '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,
        'response_size': 42,
        'response_status': 200,
        'request_header': {'H1': 'a', 'H2': 'b'},
    }

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

I meant, can it test/assert the timezone? because request_start_time is fixed, then the expected always will be the same

@NewUserHa
Copy link
Contributor Author

well. Will adding tz to datetime.datetime(1843, 1, 1, 0, 30) of test_access_logger_atoms work?

@asvetlov
Copy link
Member

I guess yes, please extract checking of %t format into a separate test.
Should be easy to do.
test_access_logger_atoms() tests too much format options.
Now I see that monkey-patching is the easiest way to check your functionality, that's fine.

tests/test_web_log.py Outdated Show resolved Hide resolved
Co-Authored-By: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
tests/test_web_log.py Outdated Show resolved Hide resolved
Co-Authored-By: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
tests/test_web_log.py Outdated Show resolved Hide resolved
tests/test_web_log.py Outdated Show resolved Hide resolved
tests/test_web_log.py Outdated Show resolved Hide resolved
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"'
@pytest.fixture
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that skip marker doesn't work on fixtures.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but if use parameterizing, it seems to have to use fixtrue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I'm talking about that the marked is supposed to skip things depending on the condition (PyPy). And if you apply it to fixtures, not test functions it just doesn't have any effect meaning it will never skip any tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I need to check some documents.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it's okay since you removed the fixture.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but...

tests/test_web_log.py Outdated Show resolved Hide resolved
Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(needs fixing to actually work)

@webknjaz webknjaz self-requested a review June 22, 2019 13:47
tests/test_web_log.py Outdated Show resolved Hide resolved
Copy link
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@webknjaz
Copy link
Member

It should be good to merge once CI jobs show up as green.

@webknjaz webknjaz changed the title Update access log use localtime and timezone instead all uctime Make access log use local time with timezone Jun 22, 2019
@webknjaz webknjaz merged commit b05e2d3 into aio-libs:master Jun 22, 2019
@webknjaz
Copy link
Member

Thanks, @NewUserHa!

@NewUserHa
Copy link
Contributor Author

NewUserHa commented Jun 22, 2019

thanks for your review and merge.
but why is test_access_logger_atoms SKIPPED in release 3.5 with running pytest against the 3.5 test_web_log.py?
need change log?

@webknjaz
Copy link
Member

Because you submitted the PR against devel which is future 4.0. 3.5 is in its own stable branch.

And yes, the changelog fragment is needed. We just missed it. So please submit a follow-up PR with it.

@NewUserHa
Copy link
Contributor Author

release 3.5 against test_web_log.py 3.5.

#3863

webknjaz pushed a commit that referenced this pull request Jun 22, 2019
@chansonzhang
Copy link

I use version 3.7.3 and the timestamp in access log still in UTC timezone

asvetlov pushed a commit that referenced this pull request Dec 16, 2020
(cherry picked from commit eb9aa22)

Co-authored-by: NewUserHa <32261870+NewUserHa@users.noreply.github.com>
@asvetlov
Copy link
Member

Thanks for the reminder. We missed to backport the PR.

#5351 should land at 3.8

asvetlov added a commit that referenced this pull request Dec 16, 2020
(cherry picked from commit eb9aa22)

Co-authored-by: NewUserHa <32261870+NewUserHa@users.noreply.github.com>

Co-authored-by: NewUserHa <32261870+NewUserHa@users.noreply.github.com>
cdce8p pushed a commit to cdce8p/aiohttp that referenced this pull request Aug 25, 2023
PR aio-libs#3860 by @NewUserHa

Co-Authored-By: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
cdce8p pushed a commit to cdce8p/aiohttp that referenced this pull request Aug 25, 2023
Dreamsorcerer pushed a commit that referenced this pull request Aug 26, 2023
## What do these changes do?
Backport #3860 to the `3.9` branch with the latest changes from `master`
Use changelog fragment from #3860.
It was only ever merged into `master` and `3.8`.

_This also resolves a DeprecationWarning for Users when running Python
3.12 -> `utcnow`._

---------

Co-authored-by: NewUserHa <32261870+NewUserHa@users.noreply.github.com>
Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

web access log show utc time instead local time with timezone.
5 participants