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(log): add logger for kazoo.handlers.threading #725

Closed
wants to merge 2 commits into from

Conversation

zqfan
Copy link

@zqfan zqfan commented Jul 21, 2023

Fixes #724

No handlers could be found for logger "kazoo.handlers.threading"

for better logging control, just like kazoo.client does

kazoo/client.py Outdated Show resolved Hide resolved
kazoo/handlers/threading.py Outdated Show resolved Hide resolved
@zqfan zqfan changed the title add logger for kazoo.handlers.threading fix(log): add logger for kazoo.handlers.threading Jul 21, 2023
@codecov
Copy link

codecov bot commented Jul 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.65%. Comparing base (92bd0c2) to head (0302623).
Report is 16 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #725      +/-   ##
==========================================
- Coverage   96.73%   96.65%   -0.09%     
==========================================
  Files          27       27              
  Lines        3556     3560       +4     
==========================================
+ Hits         3440     3441       +1     
- Misses        116      119       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zqfan zqfan requested a review from houndci-bot July 24, 2023 11:48
@ceache
Copy link
Contributor

ceache commented Feb 26, 2024

Hi @zqfan. Thank you for the PR but, I am sorry but I am not clear on the goals of this PR.
What is gained by making the handler reuse the client's logger?
Do you have a clear example?

@zqfan
Copy link
Author

zqfan commented Feb 26, 2024

Hi @zqfan. Thank you for the PR but, I am sorry but I am not clear on the goals of this PR. What is gained by making the handler reuse the client's logger? Do you have a clear example?

log.exception("Exception in worker queue thread")
This line needs log to be set, i.e., logging.basicConfig(), otherwise, it might print an error message on stderr: "No handlers could be found for logger "kazoo.handlers.threading"" . As issue #724 pointed out, it leads to some information unnoticed, and cause a lot of time to dig out.

But it might be better if wen can specify a logger handler, which allows us to print log info into a specific file. Upper level application can only deal with kazoo.client, and it is not a good idea to expose kazoo/handlers/threading.py detail to upper level just for a logger handler, so I let it inherit the logger setting in kazoo.client.

this pr keeps same behavior as before, if user don't specify a dedicated logger handler

@StephenSorriaux
Copy link
Member

Thank you for the PR.

Just like @ceache said, I am also not sure about the goal of this PR.

I understand the issue about the No handlers could be found for {logger} (which is explained in the kazoo documentation) that, I think, can be solved using a default NullHandler() for our top logger (as per the official documentation).

However, I don't think passing a logger should be the way to go.

@zqfan
Copy link
Author

zqfan commented Mar 11, 2024

Thank you for the PR.

Just like @ceache said, I am also not sure about the goal of this PR.

I understand the issue about the No handlers could be found for {logger} (which is explained in the kazoo documentation) that, I think, can be solved using a default NullHandler() for our top logger (as per the official documentation).

However, I don't think passing a logger should be the way to go.

My project doesn't configure a logging.basicConfig for everything, it adjust and control major module's behaviour separately. For now, to avoid too much message printed to my log file, I have to do something like this:

logger = logging.getLogger("kazoo.client")
logger.setLevel(logging.ERROR)  // for other module, it is INFO
for h in mymodule.logger.handlers:  // a different file for other module
    logger.addHandler(h)
self.client = KazooClient(hosts=ZK_HOSTS,logger=logger)

If kazoo.handlers.threading could not inherit logger settings from kazoo client, then I have to do it twice.
I think end user should treat kazoo as a integrated module, settings should be done only once.

@StephenSorriaux
Copy link
Member

If kazoo.handlers.threading could not inherit logger settings from kazoo client, then I have to do it twice.
I think end user should treat kazoo as a integrated module, settings should be done only once.

That can already be done: kazoo.handlers.threading does not inherit from kazoo.client but both inherit from kazoo, ie.

import logging
logging.basicConfig(level=logging.INFO)
...
logger = logging.getLogger("kazoo")
logger.setLevel(logging.ERROR)

@ceache I think the possible use case for this, which is not explained here or in the initial issue, is the same as the reason why we have a logger configuration in the client (see #85): passing a logger around to possibly discriminate between multiple instances of kazoo running on the same host or whatever. I must say I am... intrigued: isn't that something the developer must take care during the development (via the formatter, etc.)? boto3 or requests, to take some "famous" libs, do not let developers pass around their logger and I can easily understand why since Python already provides everything for that... Anyway, I would love your thoughts on this.

@jeffwidman
Copy link
Member

I understand what you're trying to achieve, but as @StephenSorriaux points out above, I think it's already solvable if you change the logger you're tuning to kazoo rather than kazoo.client.

May I recommend @brandon-rhodes excellent logging_tree library? It's been a massive time saver for me over the years when I'm debugging unfamiliar code.

Closing, but if for some reason you can't achieve this with existing tooling please comment and we can re-evaluate.

@jeffwidman jeffwidman closed this Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No handlers could be found for logger "kazoo.handlers.threading"
5 participants