Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output logs with python logging in a click cli?

I've written a python cli with the 'click' library. I'd like to also use python's built-in logging module for logging to the console. But I've struggled getting the logging messages to the console. I tried a very simple approach:

logger = logging.getLogger(__name__)

@click.command()
def cli():
    logger.setLevel("INFO")
    logger.info("Does this work?")
    print("done.")

The logger content doesn't appear in my console. Maybe it needs a handler to explicitly send log messages to stdout?

logger = logging.getLogger(__name__)

@click.command()
def cli():
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
    handler.setLevel("INFO")
    logger.addHandler(handler)
    logger.info("Does this work?")
    print("done.")

Unfortunately this also doesn't work.

A third option--creating a handler and setting loglevels for the handler and the logger-- works:

logger = logging.getLogger(__name__)

@click.command()
def cli():
    logger.setLevel("INFO")
    handler = logging.StreamHandler(sys.stderr)
    handler.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
    handler.setLevel("INFO")
    logger.addHandler(handler)
    logger.info("Does this work?")
    print("done.")

It seems like:

  • if creating a logger with logging.getLogger, I have to create a handler for my logger explicitly.
  • and I have to set a loglevel on both the logger and the handler?

Is that right? It seems silly to set the level twice. What's the point?

Or am I still misunderstanding the right way to do this?

Thanks for your help!

like image 732
NateV Avatar asked Apr 23 '20 12:04

NateV


2 Answers

I personally like the loguru library for handling logs. I think it's simpler.

Here's an example of how I usually do it:

import click
from loguru import logger


@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name', help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        logger.info(f"That's it, beautiful and simple logging! - Counter: {x}")
        click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()
❯ python loguru-click-cli-test.py --count=3
Your name: Geraldo
2020-06-10 20:02:48.297 | INFO     | __main__:hello:11 - That's it, beautiful and simple logging! - Counter: 0
Hello Geraldo!
2020-06-10 20:02:48.297 | INFO     | __main__:hello:11 - That's it, beautiful and simple logging! - Counter: 1
Hello Geraldo!
2020-06-10 20:02:48.297 | INFO     | __main__:hello:11 - That's it, beautiful and simple logging! - Counter: 2
Hello Geraldo!

Loguru: https://github.com/Delgan/loguru

like image 190
Geraldo Castro Avatar answered Oct 31 '22 23:10

Geraldo Castro


I had to manually set

logging.basicConfig(level=logging.INFO)

Example

import click
import logging

logging.basicConfig(level=logging.INFO)

@click.command()
def cli():
    logging.info("it works")

gives

$ mycli
INFO:root:it works
like image 36
gdforj Avatar answered Oct 31 '22 23:10

gdforj