Skip to content

Commit

Permalink
Reduce access log overhead when logging is disabled (#7240)
Browse files Browse the repository at this point in the history
<!-- Thank you for your contribution! -->

## What do these changes do?

If the logger was disabled the log message would be formatted and thrown
away. Avoid formatting the log message when logging is not enabled at
info level.

In testing this represented up to 12% of the run time of serving a
static file.

## Are there changes in behavior for the user?

No

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this
change? -->

## Checklist

- [x] I think the code is well written
- [x] 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.
- [x] 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."

---------

Co-authored-by: Sviatoslav Sydorenko <wk.cvs.github@sydorenko.org.ua>
Co-authored-by: Sam Bull <aa6bs0@sambull.org>
  • Loading branch information
3 people authored Apr 16, 2023
1 parent 1c49738 commit 315ae90
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/7240.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Turned access log into no-op when the logger is disabled.
3 changes: 3 additions & 0 deletions aiohttp/web_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ def _format_line(
return [(key, method(request, response, time)) for key, method in self._methods]

def log(self, request: BaseRequest, response: StreamResponse, time: float) -> None:
if not self.logger.isEnabledFor(logging.INFO):
# Avoid formatting the log line if it will not be emitted.
return
try:
fmt_info = self._format_line(request, response, time)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_web_log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# type: ignore
import datetime
import logging
import platform
import sys
from typing import Any
Expand Down Expand Up @@ -251,3 +252,14 @@ def log(self, request, response, time):
resp = await client.get("/")
assert 200 == resp.status
assert msg == "contextvars: uuid"


def test_logger_does_nothing_when_disabled(caplog: pytest.LogCaptureFixture) -> None:
"""Test that the logger does nothing when the log level is disabled."""
mock_logger = logging.getLogger("test.aiohttp.log")
mock_logger.setLevel(logging.INFO)
access_logger = AccessLogger(mock_logger, "%b")
access_logger.log(
mock.Mock(name="mock_request"), mock.Mock(name="mock_response"), 42
)
assert "mock_response" in caplog.text

0 comments on commit 315ae90

Please sign in to comment.