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

Python client should not declare any root handler (using logging.basicConfig) #7885

Closed
exalate-issue-sync bot opened this issue May 11, 2023 · 1 comment

Comments

@exalate-issue-sync
Copy link

In library mode, it is strongly advised not to declare any root handler in the library code.

Unfortunately, that’s what we do in {{h2o.py}} with the {{logging.basicConfig()}} statement.
We should then just delete this instruction.
In both Py2.7 and Py3.5+, this will just let Python raise a warning if the h2o logger is used but the user didn’t define any handler for it (nor any parent handler like the root handler…) and all logs at warning or error level will then be redirected to stderr.
This means that in this case, the {{info}} level is not logged at all.

Note that currently (strangely?), the h2o logger is used only in {{config}} module, mainly to log config errors, so I would recommend to just delete the {{logging.basicConfig()}} statement mentionned earlier.

h3. Moving further

Py client is using {{print}} in many situations where the Python logger would be more appropriate: warnings, error messages, requests response…

I think we should switch to a logger for those situations to provide the user with more control on what/where to log.
In this case, although it is not usually recommended to define handlers in libraries, we could even consider to provide a basic console handler for the h2o logger and use it only if the user didn’t define its own handlers (only for Py3):

{code:python}import logging

logger = logging.getLogger('h2o')
if PY3 and not logger.hasHandlers():
logger.setLevel(level=logging.INFO)
logger.addHandler(logging.StreamHandler()) # log to console{code}

@h2o-ops
Copy link
Collaborator

h2o-ops commented May 14, 2023

JIRA Issue Migration Info

Jira Issue: PUBDEV-7755
Assignee: Sebastien Poirier
Reporter: Sebastien Poirier
State: Resolved
Fix Version: 3.32.0.1
Attachments: N/A
Development PRs: Available

Linked PRs from JIRA

#4903

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

No branches or pull requests

1 participant