Easier way to enable verbose logging

前端 未结 7 2306
一整个雨季
一整个雨季 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:12

    Here's another take on having argparse count the -v option to increase verbosity up two levels from the default WARNING to INFO (-v) to DEBUG (-vv). This does not map to the constants defined by logging but rather calculates the value directly, limiting the input:

    print( "Verbosity / loglevel:", args.v )
    logging.basicConfig( level=10*(3-max(0,min(args.v,3))) )
    logging.debug("debug") # 10
    logging.info("info") # 20
    logging.warning("warning") # 30 - The default level is WARNING, which means that only events of this level and above will be tracked
    logging.error("error") # 40
    logging.critical("critical") # 50
    

提交回复
热议问题