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

fix: consistently use a user-provided logger #176

Merged
merged 1 commit into from
Jan 24, 2023

Conversation

rnovatorov
Copy link
Contributor

Client initializer currently accepts a logger as an optional argument. That logger is passed to the underlying paho.mqtt library and then used for logging there. However, there also exists MQTT_LOGGER, which is used internally by asyncio-mqtt. Without looking at the source code it is easy to forget that in order to enable logging both in paho.mqtt and in asyncio-mqtt it is not enough to pass a logger as an argument to Client initializer.

This PR makes asyncio-mqtt use a user-provided logger instead of MQTT_LOGGER for internal logging, so that if user decides to pass a logger to Client constructor, that logger will be used both in paho.mqtt and asyncio-mqtt.

@codecov
Copy link

codecov bot commented Jan 21, 2023

Codecov Report

Merging #176 (21cbfea) into main (1ce727f) will decrease coverage by 0.1%.
The diff coverage is 55.5%.

❗ Current head 21cbfea differs from pull request most recent head 5ca3866. Consider uploading reports for the commit 5ca3866 to get more accurate results

@@           Coverage Diff           @@
##            main    #176     +/-   ##
=======================================
- Coverage   90.5%   90.5%   -0.1%     
=======================================
  Files          7       7             
  Lines        722     721      -1     
  Branches     154     153      -1     
=======================================
- Hits         654     653      -1     
  Misses        43      43             
  Partials      25      25             
Impacted Files Coverage Δ
asyncio_mqtt/client.py 82.5% <55.5%> (-0.1%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@frederikaalund frederikaalund left a comment

Choose a reason for hiding this comment

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

Sorry for the long delay. I must have missed this PR to begin with. Thanks for the ping, @JonathanPlasse. Let me have a look! :)

This looks good to me. 👍 Well done! I only have one gripe regarding default arguments. See the review for details.

With that small change, I'm ready to merge the code.

~Frederik

@@ -233,7 +233,7 @@ def __init__( # noqa: C901
*,
username: str | None = None,
password: str | None = None,
logger: logging.Logger | None = None,
logger: logging.Logger = MQTT_LOGGER,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Prefer to use None for default values. It makes it easier to extend Client (e.g., through inheritance).

In turn, assign the actual default value inside the function itself. E.g.:

if logger is None:
    logger = MQTT_LOGGER
self._logger = logger
self._client.enable_logger(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.

Done.

Client initializer currently accepts a logger as an optional argument.
That logger is passed to the underlying paho.mqtt library and then used
for logging there. However, there also exists MQTT_LOGGER, which is used
internally by asyncio-mqtt. Without looking at the source code it is
easy to forget that in order to enable logging both in paho.mqtt and in
asyncio-mqtt it is not enough to pass a logger as an argument to Client
initializer.

This commit makes asyncio-mqtt use a user-provided logger instead of
MQTT_LOGGER for internal logging, so that if user decides to pass a
logger to Client constructor, that logger will be used both in paho.mqtt
and asyncio-mqtt.
@frederikaalund frederikaalund merged commit 2cceeb2 into empicano:main Jan 24, 2023
@frederikaalund
Copy link
Collaborator

LGTM. 👍

Thank you for your contribution to asyncio-mqtt. :)

I apologize for the unnecessarily long PR review time on my part. I completely missed this PR (must have cleared my inbox a bit too fast 😅 ).

@rnovatorov
Copy link
Contributor Author

That's not a problem at all. Thank you for the project :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants