Skip to content

Commit

Permalink
Fix logging (#393)
Browse files Browse the repository at this point in the history
* ログメッセージの変更

* tox.iniに削除

* version up

* query_paramsをマスクする
  • Loading branch information
yuji38kwmt committed Jan 5, 2022
1 parent c1a81ec commit cc16928
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 35 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- "3.7"
- "3.8"
- "3.9"
- "3.10-dev"
install:
- pip install poetry
- travis_retry poetry install
Expand Down
2 changes: 1 addition & 1 deletion annofabapi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.52.3"
__version__ = "0.52.4"
81 changes: 60 additions & 21 deletions annofabapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,35 @@ def mask_key(d, key: str):
return copied_data


def _create_query_params_for_logger(params: Dict[str, Any]) -> Dict[str, Any]:
"""
ログに出力するためのquery_paramsを生成する。
* AWS関係のcredential情報をマスクする。
Args:
params: query_params
Returns:
ログ出力用のparams
"""

def mask_key(d, key: str):
if key in d:
d[key] = "***"

MASKED_KEYS = {"X-Amz-Security-Token", "X-Amz-Credential"}
diff = MASKED_KEYS - set(params.keys())
if len(diff) == len(MASKED_KEYS):
# マスク対象のキーがない
return params

copied_params = copy.deepcopy(params)
for key in MASKED_KEYS:
mask_key(copied_params, key)

return copied_params


def _should_retry_with_status(status_code: int) -> bool:
"""HTTP Status Codeからリトライすべきかどうかを返す。"""
if status_code == 429:
Expand Down Expand Up @@ -348,23 +377,28 @@ def _execute_http_request(
requests.exceptions.HTTPError: http status codeが4XXX,5XXXのとき
"""
response = self.session.request(
method=http_method, url=url, params=params, data=data, headers=headers, json=json, **kwargs
)

# response.requestよりメソッド引数のrequest情報の方が分かりやすいので、メソッド引数のrequest情報を出力する。
logger.debug(
"Sending a request :: %s",
"Sent a request :: %s",
{
"http_method": http_method,
"url": url,
"query_params": params,
"request_body_json": _create_request_body_for_logger(json),
"request_body_data": _create_request_body_for_logger(data),
"header_params": headers,
"requests": {
"http_method": http_method,
"url": url,
"query_params": _create_query_params_for_logger(params) if params is not None else None,
"request_body_json": _create_request_body_for_logger(json) if json is not None else None,
"request_body_data": _create_request_body_for_logger(data) if data is not None else None,
"header_params": headers,
},
"response": {
"status_code": response.status_code,
"content_length": len(response.content),
},
},
)

response = self.session.request(
method=http_method, url=url, params=params, data=data, headers=headers, json=json, **kwargs
)

# リトライすべき場合はExceptionを返す
if raise_for_status or _should_retry_with_status(response.status_code):
_log_error_response(logger, response)
Expand Down Expand Up @@ -408,19 +442,24 @@ def _request_wrapper(
url = f"{self.url_prefix}{url_path}"

kwargs = self._create_kwargs(query_params, header_params, request_body)

# HTTP Requestを投げる
response = self.session.request(method=http_method.lower(), url=url, **kwargs)
# response.requestよりメソッド引数のrequest情報の方が分かりやすいので、メソッド引数のrequest情報を出力する。
logger.debug(
"Sending a request :: %s",
"Sent a request :: %s",
{
"http_method": http_method.lower(),
"url": url,
"query_params": query_params,
"header_params": header_params,
"request_body": _create_request_body_for_logger(request_body) if request_body is not None else None,
"request": {
"http_method": http_method.lower(),
"url": url,
"query_params": query_params,
"header_params": header_params,
"request_body": _create_request_body_for_logger(request_body) if request_body is not None else None,
},
"response": {
"status_code": response.status_code,
"content_length": len(response.content),
},
},
)
response = self.session.request(method=http_method.lower(), url=url, **kwargs)

# Unauthorized Errorならば、ログイン後に再度実行する
if response.status_code == requests.codes.unauthorized:
Expand Down
2 changes: 1 addition & 1 deletion annofabapi/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, login_user_id: str, login_password: str, endpoint_url: str =
#: AnnofabApi2 Instance
self.api2 = AnnofabApi2(self.api)

logger.info(
logger.debug(
"Create annofabapi resource instance :: %s", {"login_user_id": login_user_id, "endpoint_url": endpoint_url}
)

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "annofabapi"
version = "0.52.3"
version = "0.52.4"
description = "Python Clinet Library of AnnoFab WebAPI (https://annofab.com/docs/api/)"
authors = ["yuji38kwmt"]
license = "MIT"
Expand All @@ -16,6 +16,7 @@ classifiers = [
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Utilities",
"Operating System :: OS Independent",
]
Expand Down
11 changes: 0 additions & 11 deletions tox.ini

This file was deleted.

0 comments on commit cc16928

Please sign in to comment.