Repository navigation
feat(log): request/response logging decorator - #76
Conversation
Creation of a configurable decorator to log requests and their responses on the desired log level
|
|
||
|
|
||
| def log_traffic(_fn: Callable = None, | ||
| log_request: Optional[bool] = True, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I definitely had misunderstood it; thanks for the clarification
| log_response: Optional[bool] = True, | ||
| log_level: Optional[int] = 20, | ||
| ) -> Callable: | ||
| """Customizable decorator for logging requests and responses on |
There was a problem hiding this comment.
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.
| configurable log level. | ||
|
|
||
| Args: | ||
| _fn: The function to be decorated (gets passed implicitly). |
There was a problem hiding this comment.
Private arguments don't need to be described in the API docs.
|
|
||
| Args: | ||
| _fn: The function to be decorated (gets passed implicitly). | ||
| log_request: A boolean flag to set whether or not the request should |
There was a problem hiding this comment.
No need to mention types and defaults, this is obvious from the function's signature.
| if log_response: | ||
| logger.log(level=log_level, | ||
| msg=f"Response to request " | ||
| f"\"{request.environ['REQUEST_METHOD']} " |
There was a problem hiding this comment.
Unnecessary repetition. Write the request to a variable and use it in both the log request and log response sections.
| @@ -0,0 +1,78 @@ | |||
| """Decorator for logging requests and responses.""" | |||
There was a problem hiding this comment.
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.
| @@ -0,0 +1,69 @@ | |||
| """Tests for the logging decorator""" | |||
There was a problem hiding this comment.
Again - there may be tests added for other functions in the future.
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def test_log_level(caplog): |
There was a problem hiding this comment.
Better to include what you are testing in case other functions are added.
| @@ -0,0 +1,69 @@ | |||
| """Tests for the logging decorator""" | |||
|
|
|||
| import logging | |||
There was a problem hiding this comment.
Check how imports should be ordered in Python. See here for the relevant section in PEP8.
Description
Creation of a configurable decorator to log requests and their responses on the desired log level
Checklist: