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

feat(verbose_logging): exclude auth and headers from logging #26

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion request_session/request_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import time
from collections import namedtuple
from copy import deepcopy
from typing import List # pylint: disable=unused-import
from typing import Any, Callable, Dict, Optional, Tuple, Union

Expand Down Expand Up @@ -41,6 +42,9 @@ class RequestSession(object):
server error. Defaults to 0.
:param bool verbose_logging: (optional) If true, add request's parameters to event
being logged. Defaults to ``False``.
:param tuple request_param_logging_blacklist: (optional)
Request params keys that won't be logged with verbose_logging.
e.g. auth or header keys might contain secrets. Defaults to ``("auth", "headers")``.
:param str request_category: (optional) Name of the event. ``request_category`` has
to passed to the object or as an argument when calling some HTTP method.
:param bool raise_for_status: (optional) Raise an exception in case of an error.
Expand Down Expand Up @@ -73,6 +77,7 @@ def __init__(
verify=True, # type: Union[bool, str]
max_retries=0, # type: int
verbose_logging=False, # type: bool
request_param_logging_blacklist=None, # type: Optional[Tuple[str]]
headers=None, # type: Optional[Dict]
request_category=None, # type: Optional[str]
raise_for_status=True, # type: bool
Expand Down Expand Up @@ -102,6 +107,10 @@ def __init__(
self.verify = verify
self.max_retries = max_retries
self.verbose_logging = verbose_logging
self.request_param_logging_blacklist = request_param_logging_blacklist or (
"auth",
"headers",
)
self.headers = headers
self.request_category = request_category
self.raise_for_status = raise_for_status
Expand Down Expand Up @@ -471,7 +480,11 @@ def _log_with_params(
"""
extra_params = (
{
"request_params": json.dumps(request_params),
"request_params": {
k: v
for k, v in deepcopy(request_params).items()
if k not in self.request_param_logging_blacklist
},
"response_text": self.get_response_text(response),
}
if self.verbose_logging
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name="request_session",
version="0.16.0",
version="0.16.1",
url="https://github.com/kiwicom/request-session",
description="Python HTTP requests on steroids",
long_description=readme,
Expand Down
Loading