Python develops "common modules": Log modules

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.