Repository: aio-libs/aiohttp
Pull request: Make access log use local time with timezone
<!-- Thank you for your contribution! -->

## 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

- [x] 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 &lt;Name&gt; &lt;Surname&gt;.
  * 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."


Problem statement:
web access log show utc time instead local time with timezone.
## Long story short
in web_.log.py:
```
    def _format_t(request: BaseRequest,
                  response: StreamResponse,
                  time: float) -> str:
        now = datetime.datetime.utcnow()
        start_time = now - datetime.timedelta(seconds=time)
        return start_time.strftime('[%d/%b/%Y:%H:%M:%S +0000]')
```
the logged time is fixed and is a utc time, not a local time. there's not a easy way to setting it.

## Expected behaviour

```
INFO:aiohttp.access:127.0.0.1 [18/Jun/2019:04:56:22 -8]
```
## Actual behaviour

```
INFO:aiohttp.access:127.0.0.1 [18/Jun/2019:04:56:22 +0000]
```

Hints from issue discussion:
UTC time is intentional.
It's nice to make it customizable. I see it being useful during the development in particular.

@NewUserHa feel free to research improving this and send a PR.
nginx's access.log is `[16/Jun/2019:15:25:04 +0800]`

what about `making _format_t` to obey OS or python's timezone info? I doubt adding a parameter for customing will work.
What is `apache` behavior?
`127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326` from https://httpd.apache.org/docs/2.4/logs.html
Ok, I have no objections for changing defaults to use local time instead of UTC. The output should include a timezone offset like @NewUserHa mentioned: `[16/Jun/2019:15:25:04 +0800]`
I would like to PR this too. (though the other one still is not finished)...

what about obeying OS or python's timezone info? need import like pytz module for ease?
Don't follow what is the difference between OS and python TZ info?
Please no `pytz` if we can avoid the library (I think we cannot need if for local timezone-aware time).
hmm.. maybe someone has different values for OS and python TZ info.
ok.
https://github.com/aio-libs/aiohttp/pull/3860

Commit message: Update web_log.py

Patch to review:
diff --git a/aiohttp/web_log.py b/aiohttp/web_log.py
index a1a4576b401..20fa95fbd45 100644
--- a/aiohttp/web_log.py
+++ b/aiohttp/web_log.py
@@ -3,6 +3,7 @@
 import logging
 import os
 import re
+import time as _time
 from collections import namedtuple
 from typing import Callable, Dict, Iterable, List, Tuple  # noqa
 
@@ -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=-_time.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,


Code review comments:
diff_hunk: @@ -3,6 +3,7 @@
 import logging
 import os
 import re
+import time as _time
path: aiohttp/web_log.py
text: @user1:
I don't think that you need to use an alias here. Please follow the style of the surrounding code.

@author:
it's needed. the time will be that argument if no alias

@user1:
```suggestion
from time import timezone
```