Created
January 8, 2019 15:09
-
-
Save willprice/352bb7cd40de33e73b84b93b9ab3d240 to your computer and use it in GitHub Desktop.
Python argparse logging verbosity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import logging | |
parser = argparse.ArgumentParser( | |
description="Demo of setting logging verbosity using -vvvv style args", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
) | |
parser.add_argument("-v", "--verbose", dest="verbosity", action="count", default=0, | |
help="Verbosity (between 1-4 occurrences with more leading to more " | |
"verbose logging). CRITICAL=0, ERROR=1, WARN=2, INFO=3, " | |
"DEBUG=4") | |
log_levels = { | |
0: logging.CRITICAL, | |
1: logging.ERROR, | |
2: logging.WARN, | |
3: logging.INFO, | |
4: logging.DEBUG, | |
} | |
if __name__ == '__main__': | |
args = parser.parse_args() | |
logging.basicConfig(level=log_levels[args.verbosity]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MaxenceG2M can be solved easily by doing something like:
logging.basicConfig(level=log_levels[min(args.verbosity, max(log_levels.keys()))])