Skip to content

Instantly share code, notes, and snippets.

@ryanlong1004
Created January 28, 2022 23:35
Show Gist options
  • Save ryanlong1004/4cb7cee4b54efb77e528ad953e61bc0f to your computer and use it in GitHub Desktop.
Save ryanlong1004/4cb7cee4b54efb77e528ad953e61bc0f to your computer and use it in GitHub Desktop.
Python CLI Logging Option
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