Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Avoid mutating the root logger #119

Merged
merged 2 commits into from
May 2, 2018
Merged

Avoid mutating the root logger #119

merged 2 commits into from
May 2, 2018

Conversation

c-w
Copy link
Contributor

@c-w c-w commented Apr 24, 2018

Using logging.basicConfig(level=logging.INFO) mutates the root logger for the current Python interpreter. This means that any library which imports the pydocumentdb library will have its log level set to INFO which leads to unexpected behavior.

Some libraries even have tests to defend against this behavior, e.g. Celery: https://github.com/celery/celery/blob/120770929f/t/unit/conftest.py#L231-L244

To fix this unexpected behavior, this commit introduces a new logger that's specific to the endpoint discovery retry policy and only mutates the log level of that specific logger.

Using `logging.basicConfig(level=logging.INFO)` mutates the root logger
for the current Python interpreter. This means that any library which
imports the pydocumentdb library will have its log level set to INFO
which leads to unexpected behavior. Some libraries even have tests to
defend against this behavior, e.g. Celery:
https://github.com/celery/celery/blob/120770929f/t/unit/conftest.py#L231-L244

To fix this unexpected behavior, this commit introduces a new logger
that's specific to the endpoint discovery retry policy and only mutates
the log level of that specific logger.
@@ -40,7 +43,7 @@ def __init__(self, global_endpoint_manager):
self._max_retry_attempt_count = _EndpointDiscoveryRetryPolicy.Max_retry_attempt_count
self.current_retry_attempt_count = 0
self.retry_after_in_milliseconds = _EndpointDiscoveryRetryPolicy.Retry_after_in_milliseconds
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also please retain the format of the logger.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't do that originally because it adds a little bit of boilerplate, but sure thing. Emulated the full basicConfig code path in bb74225, modeled on the Python 3.6.4 implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ping @srinathnarayanan -- could you please take another look at the pull request?

@srinathnarayanan srinathnarayanan merged commit 6be607a into Azure:master May 2, 2018
@c-w c-w deleted the fix-root-logger-mutation branch May 3, 2018 04:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants