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

How can I use click_log in a project with multiple modules? #23

Open
halloleo opened this issue Apr 3, 2020 · 1 comment
Open

How can I use click_log in a project with multiple modules? #23

halloleo opened this issue Apr 3, 2020 · 1 comment

Comments

@halloleo
Copy link

halloleo commented Apr 3, 2020

I have a project with two module cli.py and doit.py.

In the main module cli.py I use

log = logging.getLogger(__name__)
click_log.basic_config(log)

as suggest and the logging in this module is perfectly controllable via the command line.

However in the other module doit the level of the logger is always set to WARNING independent of the used command line option.

Hoe can I use click_log in the second module as well?

@halloleo halloleo changed the title Hoe can I use click_log in a multiple module project? How can I use click_log in a project with multiple modules? Apr 29, 2020
@akaihola
Copy link

akaihola commented Jan 31, 2022

In principle passing the root logger to click_log should work:

root_logger = logging.getLogger()
click_log.basic_config(root_logger)

But IIRC the problem is that at this point logging has already been configured. Looking at one of our old applications, we're doing this trick when configuring logging without external tools:

def configure_logging(logconfig):
    """Sets up the logging framework according to given logging .INI file"""
    if os.path.isfile(logconfig):
        # Take over logging configuration.  This disables all logging:
        logging.config.dictConfig({'version': 1})
        # Read new logging configuration from an .ini file:
        logging.config.fileConfig(logconfig, disable_existing_loggers=False)

but I'll have to try out if this approach would work here.

Update:

This actually does work for me:

import logging

import click
import click_log

root_logger = logging.getLogger()
click_log.basic_config(root_logger)

@click.command()
@click_log.simple_verbosity_option(root_logger)
def my_command():
    ...

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

No branches or pull requests

2 participants