Skip to content

Commit

Permalink
Switch to %-style log formatting
Browse files Browse the repository at this point in the history
We should let Python's logging module lazily interpolate the string.
This is generally recommended as the way to write log messages (over
eagerly formatting with either .format() or f-strings). This has two
benefits:

1) The 'processing' required to calculate the value of the variable
being interpolated doesn't need to happen unless the log message is
actually being emitted.
2) Sentry/etc can group together error log lines based on the base
unformatted message. If we eagerly interpolate, each log message will be
unique and so they don't get grouped together.
  • Loading branch information
samuelhwilliams committed May 4, 2023
1 parent e8f972b commit 0ba200a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions notifications_python_client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def generate_headers(self, api_token):
}

def request(self, method, url, data=None, params=None):
logger.debug("API request {} {}".format(method, url))
logger.debug("API request %s %s", method, url)
url, kwargs = self._create_request_objects(url, data, params)

response = self._perform_request(method, url, kwargs)
Expand Down Expand Up @@ -93,12 +93,12 @@ def _perform_request(self, method, url, kwargs):
except requests.RequestException as e:
api_error = HTTPError.create(e)
logger.error(
"API {} request on {} failed with {} '{}'".format(method, url, api_error.status_code, api_error.message)
"API %s request on %s failed with %s '%s'", method, url, api_error.status_code, api_error.message
)
raise api_error
finally:
elapsed_time = time.monotonic() - start_time
logger.debug("API {} request on {} finished in {}".format(method, url, elapsed_time))
logger.debug("API %s request on %s finished in %s", method, url, elapsed_time)

def _process_json_response(self, response):
try:
Expand Down
2 changes: 1 addition & 1 deletion notifications_python_client/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_notification_by_id(self, id):

def get_pdf_for_letter(self, id):
url = "/v2/notifications/{}/pdf".format(id)
logger.debug("API request {} {}".format("GET", url))
logger.debug("API request %s %s", "GET", url)
url, kwargs = self._create_request_objects(url, data=None, params=None)

response = self._perform_request("GET", url, kwargs)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ select = [
"I", # isort
"B", # flake8-bugbear
"C90", # mccabe cyclomatic complexity
"G", # flake8-logging-format
]
ignore = []
exclude = [
Expand Down

0 comments on commit 0ba200a

Please sign in to comment.