Skip to content

Commit

Permalink
Use more performant version of Python logging calls (#1205)
Browse files Browse the repository at this point in the history
* log.debug: Use separate arguments not format()

It's much more performant to avoid format() strings when generating
logging messages.

* log.info: Use separate arguments not format()

* log.error/log.warning: Use separate arguments not format()

---------

Co-authored-by: Gonchik Tsymzhitov <gonchik.tsymzhitov@gmail.com>
  • Loading branch information
madscientist and gonchik authored Jul 26, 2023
1 parent fb5f845 commit 1a5a0a0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion atlassian/bitbucket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def all_project_administrators(self):
:return: A generator object containing a map with the project_key, project_name and project_administrators
"""
for project in self.project_list():
log.info("Processing project: {0} - {1}".format(project.get("key"), project.get("name")))
log.info("Processing project: %s - %s", project.get("key"), project.get("name"))
yield {
"project_key": project.get("key"),
"project_name": project.get("name"),
Expand Down
43 changes: 21 additions & 22 deletions atlassian/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def get_page_by_title(self, space, title, start=0, limit=1, expand=None, type="p
try:
return response.get("results")[0]
except (IndexError, TypeError) as e:
log.error("Can't find '{title}' page on the {url}!".format(title=title, url=self.url))
log.error("Can't find '%s' page on the %s!", title, self.url)
log.debug(e)
return None

Expand Down Expand Up @@ -745,7 +745,7 @@ def create_page(
:param full_width: DEFAULT: False
:return:
"""
log.info('Creating {type} "{space}" -> "{title}"'.format(space=space, title=title, type=type))
log.info('Creating %s "%s" -> "%s"', type, space, title)
url = "rest/api/content/"
data = {
"type": type,
Expand Down Expand Up @@ -1302,12 +1302,12 @@ def remove_page_attachment_keep_version(self, page_id, filename, keep_last_versi
version=remove_version_attachment_number,
)
log.info(
"Removed oldest version for {}, now versions equal more than {}".format(
attachment.get("title"), len(attachment_versions)
)
"Removed oldest version for %s, now versions equal more than %s",
attachment.get("title"),
len(attachment_versions),
)
attachment_versions = self.get_attachment_history(attachment.get("id"))
log.info("Kept versions {} for {}".format(keep_last_versions, attachment.get("title")))
log.info("Kept versions %s for %s", keep_last_versions, attachment.get("title"))

def get_attachment_history(self, attachment_id, limit=200, start=0):
"""
Expand Down Expand Up @@ -1506,8 +1506,8 @@ def remove_page_history_keep_version(self, page_id, keep_last_versions):
self.remove_page_history(page_id=page_id, version_number=1)
page = self.get_page_by_id(page_id=page_id, expand="version")
page_number = page.get("version").get("number")
log.info("Removed oldest version for {}, now it's {}".format(page.get("title"), page_number))
log.info("Kept versions {} for {}".format(keep_last_versions, page.get("title")))
log.info("Removed oldest version for %s, now it's %s", page.get("title"), page_number)
log.info("Kept versions %s for %s", keep_last_versions, page.get("title"))

def has_unknown_attachment_error(self, page_id):
"""
Expand Down Expand Up @@ -1536,7 +1536,7 @@ def is_page_content_is_already_updated(self, page_id, body, title=None):
if title:
current_title = confluence_content.get("title", None)
if title != current_title:
log.info("Title of {page_id} is different".format(page_id=page_id))
log.info("Title of %s is different", page_id)
return False

if self.advanced_mode:
Expand All @@ -1554,14 +1554,14 @@ def is_page_content_is_already_updated(self, page_id, body, title=None):
# @todo move into utils
confluence_body_content = utils.symbol_normalizer(confluence_body_content)

log.debug('Old Content: """{body}"""'.format(body=confluence_body_content))
log.debug('New Content: """{body}"""'.format(body=body))
log.debug('Old Content: """%s"""', confluence_body_content)
log.debug('New Content: """%s"""', body)

if confluence_body_content.strip() == body.strip():
log.info("Content of {page_id} is exactly the same".format(page_id=page_id))
log.info("Content of %s is exactly the same", page_id)
return True
else:
log.info("Content of {page_id} differs".format(page_id=page_id))
log.info("Content of %s differs", page_id)
return False

def update_existing_page(
Expand Down Expand Up @@ -1617,7 +1617,7 @@ def update_page(
"""
# update current page
params = {"status": "current"}
log.info('Updating {type} "{title}" with {parent_id}'.format(title=title, type=type, parent_id=parent_id))
log.info('Updating %s "%s" with %s', type, title, parent_id)

if not always_update and body is not None and self.is_page_content_is_already_updated(page_id, body, title):
return self.get_page_by_id(page_id)
Expand All @@ -1628,7 +1628,7 @@ def update_page(
else:
version = self.history(page_id)["lastUpdated"]["number"] + 1
except (IndexError, TypeError) as e:
log.error("Can't find '{title}' {type}!".format(title=title, type=type))
log.error("Can't find '%s' %s!", title, type)
log.debug(e)
return None

Expand Down Expand Up @@ -1699,7 +1699,7 @@ def _insert_to_existing_page(
:param top_of_page: Option to add the content to the end of page body
:return:
"""
log.info('Updating {type} "{title}"'.format(title=title, type=type))
log.info('Updating %s "%s"', type, title)
# update current page
params = {"status": "current"}

Expand Down Expand Up @@ -1768,7 +1768,7 @@ def append_page(
If False then notifications will be sent.
:return:
"""
log.info('Updating {type} "{title}"'.format(title=title, type=type))
log.info('Updating %s "%s"', type, title)

return self._insert_to_existing_page(
page_id,
Expand Down Expand Up @@ -1803,7 +1803,7 @@ def prepend_page(
If False then notifications will be sent.
:return:
"""
log.info('Updating {type} "{title}"'.format(title=title, type=type))
log.info('Updating %s "%s"', type, title)

return self._insert_to_existing_page(
page_id,
Expand Down Expand Up @@ -1866,10 +1866,9 @@ def update_or_create(
)

log.info(
"You may access your page at: {host}{url}".format(
host=self.url,
url=((result or {}).get("_links") or {}).get("tinyui"),
)
"You may access your page at: %s%s",
self.url,
((result or {}).get("_links") or {}).get("tinyui"),
)
return result

Expand Down
32 changes: 15 additions & 17 deletions atlassian/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,13 @@ def get_component_related_issues(self, component_id):
return self.get(url)

def create_component(self, component):
log.warning('Creating component "{name}"'.format(name=component["name"]))
log.warning('Creating component "%s"', component["name"])
base_url = self.resource_url("component")
url = "{base_url}/".format(base_url=base_url)
return self.post(url, data=component)

def delete_component(self, component_id):
log.warning('Deleting component "{component_id}"'.format(component_id=component_id))
log.warning('Deleting component "%s"', component_id)
base_url = self.resource_url("component")
return self.delete("{base_url}/{component_id}".format(base_url=base_url, component_id=component_id))

Expand Down Expand Up @@ -1249,20 +1249,20 @@ def issue_exists(self, issue_key):
try:
resp = self.issue(issue_key, fields="*none")
if resp.status_code == 404:
log.info('Issue "{issue_key}" does not exists'.format(issue_key=issue_key))
log.info('Issue "%s" does not exists', issue_key)
return False
resp.raise_for_status()
log.info('Issue "{issue_key}" exists'.format(issue_key=issue_key))
log.info('Issue "%s" exists', issue_key)
return True
finally:
self.advanced_mode = original_value

def issue_deleted(self, issue_key):
exists = self.issue_exists(issue_key)
if exists:
log.info('Issue "{issue_key}" is not deleted'.format(issue_key=issue_key))
log.info('Issue "%s" is not deleted', issue_key)
else:
log.info('Issue "{issue_key}" is deleted'.format(issue_key=issue_key))
log.info('Issue "%s" is deleted', issue_key)
return not exists

def delete_issue(self, issue_id_or_key, delete_subtasks=True):
Expand All @@ -1283,13 +1283,13 @@ def delete_issue(self, issue_id_or_key, delete_subtasks=True):
else:
params["deleteSubtasks"] = "false"

log.warning("Removing issue {}...".format(issue_id_or_key))
log.warning("Removing issue %s...", issue_id_or_key)

return self.delete(url, params=params)

# @todo merge with edit_issue method
def issue_update(self, issue_key, fields):
log.warning('Updating issue "{issue_key}" with "{fields}"'.format(issue_key=issue_key, fields=fields))
log.warning('Updating issue "%s" with "%s"', issue_key, fields)
base_url = self.resource_url("issue")
url = "{base_url}/{issue_key}".format(base_url=base_url, issue_key=issue_key)
return self.put(url, data={"fields": fields})
Expand Down Expand Up @@ -1323,7 +1323,7 @@ def issue_add_watcher(self, issue_key, user):
:param user:
:return:
"""
log.warning('Adding user {user} to "{issue_key}" watchers'.format(issue_key=issue_key, user=user))
log.warning('Adding user %s to "%s" watchers', user, issue_key)
data = user
base_url = self.resource_url("issue")
return self.post(
Expand All @@ -1338,7 +1338,7 @@ def issue_delete_watcher(self, issue_key, user):
:param user:
:return:
"""
log.warning('Deleting user {user} from "{issue_key}" watchers'.format(issue_key=issue_key, user=user))
log.warning('Deleting user %s from "%s" watchers', user, issue_key)
params = {"username": user}
base_url = self.resource_url("issue")
return self.delete(
Expand Down Expand Up @@ -1424,7 +1424,7 @@ def create_issues(self, list_of_issues_data):

# @todo refactor and merge with create_issue method
def issue_create(self, fields):
log.warning('Creating issue "{summary}"'.format(summary=fields["summary"]))
log.warning('Creating issue "%s"', fields["summary"])
url = self.resource_url("issue")
return self.post(url, data={"fields": fields})

Expand All @@ -1437,10 +1437,10 @@ def issue_create_or_update(self, fields):
return self.issue_create(fields)

if self.issue_deleted(issue_key):
log.warning('Issue "{issue_key}" deleted, skipping'.format(issue_key=issue_key))
log.warning('Issue "%s" deleted, skipping', issue_key)
return None

log.info('Issue "{issue_key}" exists, will update'.format(issue_key=issue_key))
log.info('Issue "%s" exists, will update', issue_key)
fields.pop("issuekey", None)
return self.issue_update(issue_key, fields)

Expand Down Expand Up @@ -1804,7 +1804,7 @@ def user_create(self, username, email, display_name, password=None, notification
Default:false.
:return:
"""
log.warning("Creating user {}".format(display_name))
log.warning("Creating user %s", display_name)
data = {
"name": username,
"emailAddress": email,
Expand Down Expand Up @@ -2771,9 +2771,7 @@ def create_issue_link(self, data):
}
:return:
"""
log.info(
"Linking issue {inward} and {outward}".format(inward=data["inwardIssue"], outward=data["outwardIssue"])
)
log.info("Linking issue %s and %s", data["inwardIssue"], data["outwardIssue"])
url = self.resource_url("issueLink")
return self.post(url, data=data)

Expand Down
4 changes: 2 additions & 2 deletions atlassian/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def request(
)
response.encoding = "utf-8"

log.debug("HTTP: {} {} -> {} {}".format(method, path, response.status_code, response.reason))
log.debug("HTTP: Response text -> {}".format(response.text))
log.debug("HTTP: %s %s -> %s %s", method, path, response.status_code, response.reason)
log.debug("HTTP: Response text -> %s", response.text)
if self.advanced_mode or advanced_mode:
return response

Expand Down

0 comments on commit 1a5a0a0

Please sign in to comment.