Easier way to enable verbose logging

前端 未结 7 2308
一整个雨季
一整个雨季 2020-11-30 18:57

I want to add a debug print statement test, if I enable --verbose from the command line and if I have the following in the script.

logger.info(\         


        
7条回答
  •  一个人的身影
    2020-11-30 19:20

    Here is a more concise method, that does bounds checking, and will list valid values in help:

    parser = argparse.ArgumentParser(description='This is a demo.')
    parser.add_argument("-l", "--log", dest="logLevel", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help="Set the logging level")
    
    args = parser.parse_args()
    if args.logLevel:
        logging.basicConfig(level=getattr(logging, args.logLevel))
    

    Usage:

    demo.py --log DEBUG
    

提交回复
热议问题