Python Logging module usage

Source: Internet
Author: User

http://blog.csdn.net/zyz511919766/article/details/25136485/

Import Logging  logging.debug ('debug message')  Logging.info ( ' Info Message ' )  logging.warning ('warning message')  Logging.error (  'error message')  logging.critical ('  Critical message')  

This, directly opens the log information on the screen, and only prints warning and above information,

Log level level critical > ERROR > WARNING > INFO > DEBUG > NOTSET

Import Logging Logging.basicconfig ( level=logging. DEBUG, Format='% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s', Datefmt='%a,%d%b%Y%h:%m:%s', filename='E:/tmp/test.log', FileMode='W') Logging.debug ('Debug Message') Logging.info ('Info Message') logging.warning ('warning Message') Logging.error ('error Message') logging.critical ('Critical Message')

This, the log output to Test.log,

Alternatively, you can create an instance of logging logger

#coding: utf-8Import Logging # Create a logger logger=Logging.getlogger () logger1= Logging.getlogger ('MyLogger') Logger1.setlevel (logging. DEBUG) Logger2= Logging.getlogger ('MyLogger') Logger2.setlevel (logging.info) Logger3= Logging.getlogger ('Mylogger.child1') Logger3.setlevel (logging. WARNING) Logger4= Logging.getlogger ('mylogger.child1.child2') Logger4.setlevel (logging. DEBUG) Logger5= Logging.getlogger ('mylogger.child1.child2.child3') Logger5.setlevel (logging. DEBUG) # Create a handler for writing to the log file FH= Logging. Filehandler ('/tmp/test.log'# Create another handler for output to the console ch=logging. Streamhandler () # defines the output format of the handler formatter formatter= Logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') Fh.setformatter (formatter) ch.setformatter (formatter) #定义一个filter #filter= Logging. Filter ('mylogger.child1.child2') #fh. addfilter (filter) # Add handler #logger to logger. addfilter (filter) Logger.addhandler (FH) Logger.addhandle R (CH) #logger1. addfilter (filter) Logger1.addhandler (FH) logger1.addhandler (CH) logger2.addhandler (FH) Logger2.add Handler (CH) #logger3. addfilter (filter) Logger3.addhandler (FH) logger3.addhandler (CH) #logger4. addfilter (Filter) L Ogger4.addhandler (FH) logger4.addhandler (CH) logger5.addhandler (FH) Logger5.addhandler (CH) # Record a log Logger.debu G ('Logger Debug Message') Logger.info ('Logger Info Message') logger.warning ('Logger warning message') Logger.error ('Logger error message') logger.critical ('Logger Critical Message') Logger1.debug ('logger1 Debug Message') Logger1.info ('logger1 Info Message') logger1.warning ('logger1 warning Message') Logger1.error ('logger1 Error message') logger1.critical ('logger1 Critical Message') Logger2.debug ('logger2 Debug Message') Logger2.info ('LOGGER2 Info Message') logger2.warning ('logger2 warning Message') Logger2.error ('logger2 Error message') logger2.critical ('logger2 Critical Message') Logger3.debug ('logger3 Debug Message') Logger3.info ('LOGGER3 Info Message') logger3.warning ('LOGGER3 warning Message') Logger3.error ('LOGGER3 Error message') logger3.critical ('logger3 Critical Message') Logger4.debug ('logger4 Debug Message') Logger4.info ('LOGGER4 Info Message') logger4.warning ('LOGGER4 warning Message') Logger4.error ('logger4 Error message') logger4.critical ('logger4 Critical Message') Logger5.debug ('LOGGER5 Debug Message') Logger5.info ('LOGGER5 Info Message') logger5.warning ('LOGGER5 warning Message') Logger5.error ('LOGGER5 Error message') logger5.critical ('LOGGER5 Critical Message')

and bound two events, respectively output the console and the file inside

Python logging module usage

Related Article

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.