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 coverage pyversions Conda Version MIT License

GitHub | PyPI | Issues | Changelog

click-loglevel provides a LogLevel parameter type for use in Click and asyncclick 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.

Starting in version 0.4.0, shell completion of log level names (both built-in and custom) is also supported.

Installation

click-loglevel requires Python 3.8 or higher. Just use pip for Python 3 (You have pip, right?) to install it:

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="INFO",
    help="Set logging level",
    show_default=True,
)
def main(log_level: int) -> None:
    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
[INFO    ] Log level set to 20
$ 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="INFO",
    help="Set logging level",
    show_default=True,
)
def main(log_level: int) -> None:
    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.6.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

click_loglevel-0.6.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file click_loglevel-0.6.1.tar.gz.

File metadata

  • Download URL: click_loglevel-0.6.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for click_loglevel-0.6.1.tar.gz
Algorithm Hash digest
SHA256 9591fc4bee04b4eb9bde151f9bc756b970f5242161b3eab493af977d4b00bd46
MD5 ecebcdd875ece087866d83866e3bea3b
BLAKE2b-256 1a3d3faf1355e4daf21b0dcb04efe4aa212a82877e610afaf04a74b2266f0ce5

See more details on using hashes here.

File details

Details for the file click_loglevel-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: click_loglevel-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for click_loglevel-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9870a9713d386522a3ef159bb4d8f524844fba1cc7fe192a71666ef2fac489f9
MD5 44a4e8ad8159f74e13e45cdaeed39fb7
BLAKE2b-256 69ea5defe5e99e7a0f96a58d47ed7b13343a10841cbf0a8817c2e161b7c5a9e6

See more details on using hashes here.

Supported by

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