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: add support to log commit stats #205

Merged
merged 9 commits into from Feb 23, 2021
12 changes: 1 addition & 11 deletions google/cloud/spanner_v1/database.py
Expand Up @@ -237,12 +237,6 @@ def logger(self):

ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
ch.setFormatter(formatter)

self._logger.addHandler(ch)
return self._logger

Expand Down Expand Up @@ -657,11 +651,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self._batch.commit(return_commit_stats=self._database.log_commit_stats)
finally:
if self._database.log_commit_stats:
self._database.logger.info(
"Transaction mutation count: {}".format(
self._batch.commit_stats.mutation_count
)
)
self._database.logger.info(self._batch.commit_stats)
self._database._pool.put(self._session)


Expand Down
6 changes: 1 addition & 5 deletions google/cloud/spanner_v1/session.py
Expand Up @@ -358,11 +358,7 @@ def run_in_transaction(self, func, *args, **kw):
raise
else:
if self._database.log_commit_stats:
self._database.logger.info(
"Transaction mutation count: {}".format(
txn.commit_stats.mutation_count
)
)
self._database.logger.info(txn.commit_stats)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wnet over this in my other comment, but I'll repeat it here too: The docs for Python Logging say they expect the main msg argument to be a string. If you want to add a custom object, see the extra argument

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's exactly what I want. I've updated the call to use a formatted message and pass the CommitStats via the extra argument. Thank you for your patience while I learn how to use loggers correctly 👍

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem! Yeah this now LGTM from a logging perspective

return return_value


Expand Down