Print statements'll get you a long-in monitoring the behavior of your application, but logging'll get your further . Learn how to implement logging-lesson to generate INFO, WARNING, ERROR, and DEBUG logs for your application.
ImportSYSImportgetoptImportLogging#Pass In:python3 my_log.py-l info#Get command line Options#short:l:#long: [log=]opts, args = Getopt.getopt (sys.argv[1:],"L:", ["log="])Print("opts", opts)#[('-l ', ' info ')]Print("args", args)#[]#default log levelLog_level="INFO" forOPT, arginchOPTs#opt:-L, Arg:info ifOptinch("- L","--log"): Log_level=getattr (Logging, Arg.upper ()) logging.basicconfig (filename="./demo.log", Level=log_level, format='% (asctime) s% (levelname) s:% (message) s') forIinchRange (0, 100): ifI% 5 = =0:logging.debug ('Found A number divisible by 5: {0}'. Format (i))Else: Logging.info ('At number {0}'. Format (i)) logging.warning ('Finished sequence')
[Python] Create a Log for your Python application