Skip to content

feat(log): request/response logging decorator - #76

Merged
uniqueg merged 4 commits into
devfrom
logging-decorator
Jul 12, 2020
Merged

uniqueg merged 4 commits into
devfrom
logging-decorator

Conversation

@stikos

@stikos stikos commented Jul 9, 2020 •

Copy link
Copy Markdown
Contributor

Description

Creation of a configurable decorator to log requests and their responses on the desired log level

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not reduced the existing code coverage

Creation of a configurable decorator to log requests and their responses on the desired log level
@stikos
stikos requested a review from uniqueg July 9, 2020 16:08

@uniqueg uniqueg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@stikos Looks great, just minor details. I've done the changes already, comments are just FYI

Comment thread foca/utils/logging.py Outdated


def log_traffic(_fn: Callable = None,
log_request: Optional[bool] = True,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you may have misunderstood the meaning of Optional[]. It doesn't have anything to do whether an argument is required - it says that the type of the argument can be either None or the type indicated in between the angular brackets.

In fact, the only argument that has an Optional[] type is _fn (which can be a callable or None) - whereas all the others are always either bool (log_request and log_response) or int (log_level).

To be more explicit, I have changed the log_level default from 20 to logging.INFO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I definitely had misunderstood it; thanks for the clarification

Comment thread foca/utils/logging.py Outdated
log_response: Optional[bool] = True,
log_level: Optional[int] = 20,
) -> Callable:
"""Customizable decorator for logging requests and responses on

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to point out that a function is customizable - this is obvious from the argument section. Similarly, no need to point out arguments explicitly in the summary line.

Comment thread foca/utils/logging.py Outdated
configurable log level.

Args:
_fn: The function to be decorated (gets passed implicitly).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Private arguments don't need to be described in the API docs.

Comment thread foca/utils/logging.py Outdated

Args:
_fn: The function to be decorated (gets passed implicitly).
log_request: A boolean flag to set whether or not the request should

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to mention types and defaults, this is obvious from the function's signature.

Comment thread foca/utils/logging.py Outdated
if log_response:
logger.log(level=log_level,
msg=f"Response to request "
f"\"{request.environ['REQUEST_METHOD']} "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unnecessary repetition. Write the request to a variable and use it in both the log request and log response sections.

Comment thread foca/utils/logging.py Outdated
@@ -0,0 +1,78 @@
"""Decorator for logging requests and responses."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There may be more logging utilities added to this module in the future, so better stay generic here. The decorator is described in its own docstring.

Comment thread tests/utils/test_logging.py Outdated
@@ -0,0 +1,69 @@
"""Tests for the logging decorator"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again - there may be tests added for other functions in the future.

Comment thread tests/utils/test_logging.py Outdated
logger = logging.getLogger(__name__)


def test_log_level(caplog):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Better to include what you are testing in case other functions are added.

@@ -0,0 +1,69 @@
"""Tests for the logging decorator"""

import logging

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Check how imports should be ordered in Python. See here for the relevant section in PEP8.

@uniqueg uniqueg changed the title feat(logs): traffic logging decorator feat(log): traffic logging decorator Jul 12, 2020
@uniqueg uniqueg changed the title feat(log): traffic logging decorator feat(log): request/response logging decorator Jul 12, 2020
@uniqueg
uniqueg merged commit 3827fa0 into dev Jul 12, 2020
@uniqueg
uniqueg deleted the logging-decorator branch July 12, 2020 14:59
@uniqueg uniqueg mentioned this pull request Nov 14, 2020
10 tasks done
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.

2 participants