click-log icon indicating copy to clipboard operation
click-log copied to clipboard

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

Open halloleo opened this issue 4 years ago • 1 comments

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 avatar Apr 03 '20 07:04 halloleo

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():
    ...

akaihola avatar Jan 31 '22 15:01 akaihola