Skip to main content

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 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

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.

AsyncLogLevel

Like LogLevel, but for use with asyncclick instead of normal Click. AsyncLogLevel must be imported from the click_loglevel.asyncclick module.

Release files for click-loglevel 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for click-loglevel 0.7.0
File Size Uploaded
click_loglevel-0.7.0.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for click-loglevel 0.7.0
File Interpreter ABI Platform
click_loglevel-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / click_loglevel-0.7.0.tar.gz

Download URL click_loglevel-0.7.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a0a623fbe35d87f65ae266f8ab27dcc7e7de1015c0b2bdca45c57f3ef01f61f5
BLAKE2b-256 checksum
How to use checksums
4f1e3e9e7836e1a21486a71045a654ef2ff8c6a0d72f331ddefd9ec0d11b3b9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / click_loglevel-0.7.0-py3-none-any.whl

Download URL click_loglevel-0.7.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f7ca7af45545c17ecfcbfcaa469667016652bdc859dbeb104cee7503ab0f7b6
BLAKE2b-256 checksum
How to use checksums
4cfe1850599c1a186c83901883d6a7aa126e04860dbe319447da0c91d1746b2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page