Use Python logging to record log information such as Debug
Debug above information written to file
Info above info output in console
Import Osimport loggingvde_logging_name = "vde_logging" Log_file_path = Os.path.join (Os.path.dirname (__file__), "Vde_ Regression.log ") def logger_initialization (): # 1. Logging logger = Logging.getlogger (vde_logging_name) logger.setlevel (logging. DEBUG) # 2.handler # file Handler fh = logging. Filehandler (Log_file_path, mode= "W") Fh.setlevel (logging. DEBUG) # Standard control Console ch = logging. Streamhandler () Ch.setlevel (logging.info) # 3.format formatter = logging. Formatter ("[% (asctime) s% (name) s].% ( LevelName) S:% (message) S ") Fh.setformatter (formatter) ch.setformatter (formatter) logger.addhandler (FH) logger. AddHandler (CH) def debug_logging_recoder (debug_msg): vde_logging = Logging.getlogger (vde_logging_name) vde_logging.d Ebug (DEBUG_MSG) def info_logging_recoder (info_msg): vde_logging = Logging.getlogger (vde_logging_name) vde_logging.in FO (info_msg) if __name__ = = ' __main__ ': Logger_initialization () debug_logging_recoder ("Debug") Info_logging_recoder (" info ")
References:
http://www.zlovezl.cn/articles/replacing-print-simple-introduction-to-logging/
https://blog.igevin.info/posts/python-log/
Python Logging Tools