-
Notifications
You must be signed in to change notification settings - Fork 0
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(logging): make the verbosity option more eager #6
Conversation
This could be a solution but I have not tested it fully. There seems to be some weird behavior when the verbosity option is not specified. |
The config option and parameter are also set as "eager" so I don't know if the verbosity option will be the first to be handled in all cases. |
Amazing work, thx! Does this require any change on the current setup? |
This change requires only one optional change in the setup to handle an "edge-case" when the user provides no Otherwise, this works as long as:
Here is a minimal example (run # app.py
import clapper.click
import clapper.logging
import click
main_logger = clapper.logging.setup("awesome") # First thing: setup the logger
# Optional: ensure the default level is at "ERROR" in configs when no `-v` is given
main_logger.setLevel("ERROR")
@click.command(
entry_point_group="cli_group",
cls=clapper.click.ConfigCommand,
)
@click.option(
"--my-config",
"-c",
"my_config",
entry_point_group="config_group",
cls=clapper.click.ResourceOption,
)
@clapper.click.verbosity_option( # The logger level is set here when -v is given
main_logger,
expose_value=False,
)
def awesome_cli(my_config: list[str], **kwargs):
"""My Awesome CLI with config files loading."""
main_logger.error("App error sample (all is well).")
main_logger.warning("App warning sample (all is well).")
main_logger.info(f"App info sample: {my_config=}")
main_logger.debug("App debug sample: Now exiting CLI app.")
if __name__ == "__main__":
awesome_cli() # config1.py
import logging
conf_logger = logging.getLogger("awesome.config1")
conf_logger.error("Config error sample (all is well)")
conf_logger.warning("Config warning sample (all is well)")
conf_logger.info("Config info sample: Loading config1")
conf_logger.debug("Config debug sample: setting the my_config variable")
my_config = ["Hello", "there"] |
OK. Are you prep'ing a release soon? We're using packages from conda-forge to depend on this library. |
Sure, I can make a release by the end of the week. The CI should be fixed now, and this fix looks sufficient to solve the problem of the config's logging level. |
Does this branch need to be rebased? |
Ensure the verbosity option is handled before the configs are loaded so the logger level is set to the user's value in the config code.
13892b0
to
2098ee8
Compare
Ah, yes, I just did the rebase. |
I will add a few tests for that case and we should be done then. |
Test a command using a config file producing logs with various levels of logging and ensure the output contains only the expected messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Ensure the verbosity option is handled before the configs are loaded so the logger level is set to the user's value in the config code.
Fixes #5.