Skip to main content

For easy configuration of `logging.Logger`s with `argparse.Action`s.

Project description

pypi github

logging-actions

For easy configuration of logging.Loggers with argparse.Actions.

Use it like this:

>>> from logging_actions import log_level_action
>>> from argparse import ArgumentParser
>>> import logging

>>> logger = logging.getLogger("foo") # Create your script's logger
>>> logger.addHandler(logging.StreamHandler())  # Don't forget to add a handler!

>>> parser = ArgumentParser()
>>> _ = parser.add_argument("--log-level", action=log_level_action(logger)) # create an action for your module's logger
>>> args = parser.parse_args() # Your logger's level will be set for you when parsing

Features

Choices are handled transparently

foo.py --help

usage: foo.py [-h] [--log-level {critical,fatal,error,warn,warning,info,debug,notset}]

optional arguments:
  -h, --help            show this help message and exit
  --log-level {critical,fatal,error,warn,warning,info,debug,notset}
                        Set the logging level for foo.

If you specify a default the log-level will be set accordingly

parser.add_argument("-l", "--log-level", action=log_level_action(logger), default="info")

Custom levels are supported

logging.addLevelName(5, "TRACE")
parser.add_argument("-l", action=log_level_action(logger), default="trace")

Benefits

A better logging pattern

This replaces the following pattern for setting script log levels

from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument(
    "--log-level",
    default=logging.INFO,
    type=lambda x: getattr(logging, x)),
    help="Configure the logging level.",
)
args = parser.parse_args()
logging.basicConfig(level=args.log_level)

Manage multiple loggers easily

parser.add_argment("--foo-log-level", action=log_level_action(foo_logger))
parser.add_argment("--bar-log-level", action=log_level_action(bar_logger))

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

logging-actions-0.1.7.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

logging_actions-0.1.7-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file logging-actions-0.1.7.tar.gz.

File metadata

  • Download URL: logging-actions-0.1.7.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.10.60.1-microsoft-standard-WSL2

File hashes

Hashes for logging-actions-0.1.7.tar.gz
Algorithm Hash digest
SHA256 aa6247b17baab17c51a3fbb926cc6b30220d180e038471f0df367c56b5a27a22
MD5 852ce53918d7f9109261c46dc7af411a
BLAKE2b-256 ec98fea06a502033a285b24535e698bc05cf388f048de8bf3b0b7682cd577046

See more details on using hashes here.

File details

Details for the file logging_actions-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: logging_actions-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.10.60.1-microsoft-standard-WSL2

File hashes

Hashes for logging_actions-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c17b871acba4a0279b98c1cf0d3d16f7d78d7e816308fff3e14933ec931b3ec0
MD5 e881b579ac4c6d180cd8892fa234db0b
BLAKE2b-256 0c7d9ec492fda20df7ce6d3c6efbb6773d8ad2535e7cfbb563bc3fe7b4913104

See more details on using hashes here.

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