Setup process for the log module
ImportLogging fromLoggingImporthandlersclassIgnorebackuplogfilter (logging. Filter):defFilter (self, record):#fixed notation return 'DB Backup' not inchrecord.getmessage ()#1. Generating Logger objectsLogger = Logging.getlogger ("Web") Logger.setlevel (logging.info) Logger.addfilter (Ignorebackuplogfilter ())#2. Generating Handler ObjectsCh=logging. Streamhandler ()#ch.setlevel (logging.info)#fh = logging. Filehandler (' Web.log ')#fh = handlers. Rotatingfilehandler (' Web.log ', maxbytes=10, backupcount=3) #以文件的大小生成日志文件, up to 3FH = handlers. Timedrotatingfilehandler ('Add.log', when='S', interval=5,backupcount=3) #以5秒为周期生成日志文件, up to 3#Fh.setlevel (logging. WARNING)#2.1 Bind the handler object to the loggerlogger.addhandler (CH) logger.addhandler (FH)#3. Generating Formatter ObjectsFile_formatter = logging. Formatter ('% (asctime) s:% (levelname) s:% (name) s:% (message) s') Console_formatter=logging. Formatter ('% (asctime) s:% (levelname) s:% (filename) s:% (Lineno) s,% (message) s')#3.1 Bind the formatter object to the handlerCh.setformatter (Console_formatter) fh.setformatter (file_formatter) logger.debug ('Debug This is') logger.warning ('Warning is this') Logger.info ('info is this db backup')#Global If the level is not set, the default warning, and the highest priority level
Python develops "common modules": Log modules