Skip to main content

Log level parameter type for Click

Project description

Project Status: Active — The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/click-loglevel/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/click-loglevel.svg MIT License

GitHub | PyPI | Issues | Changelog

click-loglevel provides a LogLevel parameter type for use in Click programs that wish to let the user set the logging level. It accepts all of the logging log level names (CRITICAL, ERROR, WARNING, INFO, DEBUG, and NOTSET, all case insensitive), and converts them into their corresponding numeric values. It also accepts integer values and leaves them as-is. Custom log levels are also supported.

Installation

click-loglevel requires Python 3.6 or higher. Just use pip for Python 3 (You have pip, right?) to install click-loglevel and its dependencies:

python3 -m pip install click-loglevel

Examples

myscript.py:

import logging
import click
from click_loglevel import LogLevel

@click.command()
@click.option("-l", "--log-level", type=LogLevel(), default=logging.INFO)
def main(log_level):
    logging.basicConfig(
        format="[%(levelname)-8s] %(message)s",
        level=log_level,
    )
    logging.log(log_level, "Log level set to %r", log_level)

if __name__ == "__main__":
    main()

Running myscript.py:

$ python3 myscript.py --log-level DEBUG
[DEBUG   ] Log level set to 10
$ python3 myscript.py --log-level error
[ERROR   ] Log level set to 40
$ python3 myscript.py --log-level 15
[Level 15] Log level set to 15

Script with custom log levels:

import logging
import click
from click_loglevel import LogLevel

logging.addLevelName(15, "VERBOSE")
logging.addLevelName(25, "NOTICE")

@click.command()
@click.option(
    "-l", "--log-level",
    type=LogLevel(extra=["VERBOSE", "NOTICE"]),
    default=logging.INFO,
)
def main(log_level):
    logging.basicConfig(
        format="[%(levelname)-8s] %(message)s",
        level=log_level,
    )
    logging.log(log_level, "Log level set to %r", log_level)

if __name__ == "__main__":
    main()

API

The click_loglevel module contains a single class:

LogLevel

A subclass of click.ParamType that accepts the standard logging level names (case insensitive) and converts them to their corresponding numeric values. It also accepts integer values and leaves them as-is.

Custom log levels can be added by passing them as the extra argument to the constructor. extra can be either an iterable of level names (in which case the levels must have already been defined — typically at the module level — by calling logging.addLevelName()) or a mapping from level names to their corresponding values. All custom log levels will be recognized case insensitively; if two different level names differ only in case, the result is undefined.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

click-loglevel-0.3.0.tar.gz (6.9 kB view hashes)

Uploaded Source

Built Distribution

click_loglevel-0.3.0-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page