logging-actions
For easy configuration of logging.Logger
s with argparse.Action
s.
Use it like this:
>>> from logging_actions import log_level_action
>>> from argparse import ArgumentParser
>>> import logging
>>> logger = logging.getLogger("foo")
>>> logger.addHandler(logging.StreamHandler())
>>> parser = ArgumentParser()
>>> _ = parser.add_argument("--log-level", action=log_level_action(logger))
>>> args = parser.parse_args()
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))