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(logging): make the verbosity option more eager #6

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

Yannick-Dayer
Copy link
Collaborator

@Yannick-Dayer Yannick-Dayer commented Sep 20, 2024

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.

@Yannick-Dayer
Copy link
Collaborator Author

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.

@Yannick-Dayer
Copy link
Collaborator Author

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.

@anjos
Copy link
Member

anjos commented Sep 21, 2024

Amazing work, thx! Does this require any change on the current setup?

@Yannick-Dayer
Copy link
Collaborator Author

This change requires only one optional change in the setup to handle an "edge-case" when the user provides no -v: to ensure the logger level is at "ERROR" (as expected with no -v given), the default logger level must be specified at its creation (see example below).

Otherwise, this works as long as:

  • You use clapper.click.verbosity_option (it sets the logger level itself);
  • You name the loggers appropriately (starting with the same prefix.) in the app and the config;
  • The "main" logger is set up before the main function, to exist when the config is loaded.

Here is a minimal example (run python app.py -c config1.py -v with different amounts of -v):

# 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"]

@anjos
Copy link
Member

anjos commented Sep 25, 2024

OK. Are you prep'ing a release soon? We're using packages from conda-forge to depend on this library.

@Yannick-Dayer
Copy link
Collaborator Author

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.

@anjos
Copy link
Member

anjos commented Sep 25, 2024

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.
Copy link

Coverage report

This PR does not seem to contain any modification to coverable code.

@Yannick-Dayer
Copy link
Collaborator Author

Ah, yes, I just did the rebase.

@Yannick-Dayer
Copy link
Collaborator Author

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.
@Yannick-Dayer Yannick-Dayer marked this pull request as ready for review September 26, 2024 13:02
Copy link
Member

@anjos anjos left a comment

Choose a reason for hiding this comment

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

LGTM

@anjos anjos merged commit d17d303 into main Sep 26, 2024
10 checks passed
@anjos anjos deleted the fix/config-log-level branch September 26, 2024 16:21
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.

Config file loading does not respect CLI verbosity settings
2 participants