Created
January 28, 2022 23:35
-
-
Save ryanlong1004/4cb7cee4b54efb77e528ad953e61bc0f to your computer and use it in GitHub Desktop.
Python CLI Logging Option
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 logging | |
def handle_logging(args): | |
levels = { | |
"critical": logging.CRITICAL, | |
"error": logging.ERROR, | |
"warn": logging.WARNING, | |
"warning": logging.WARNING, | |
"info": logging.INFO, | |
"debug": logging.DEBUG, | |
} | |
level = levels.get(args.log.lower()) | |
if level is None: | |
raise ValueError( | |
f"log level given: {args.log}" | |
f" -- must be one of: {' | '.join(levels.keys())}" | |
) | |
if level is None: | |
raise ValueError( | |
f"log level given: {args.log}" | |
f" -- must be one of: {' | '.join(levels.keys())}" | |
) | |
LOG_FORMAT = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s" | |
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT) | |
if __name__ == "__main__": | |
# args collected from argparse | |
handle_logs(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment