Python's logging log module

Source: Internet
Author: User

Logging

A module for easy logging tangent thread security

Vim log_test.py

Import logginglogging.basicconfig (filename= ' Log.log ',                    format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s :  % (message) s ',                    datefmt= '%y-%m-%d%h:%m:%s%p ',                    level=logging. Debug) logging.debug (' Debug ') logging.info (' info ') logging.warning (' Warning ') logging.error (' ERROR ') Logging.critical (' critical ') logging.log (' log ')

Run the build log file Log.log

Simulate a script that generates an error log

Import logginglogging.basicconfig (filename= ' Log.log ',                    format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s :  % (message) s ',                    datefmt= '%y-%m-%d%h:%m:%s%p ',                    level=logging. DEBUG) while True:    option = raw_input ("Input a digit:")    if Option.isdigit ():        print "hehe", option        Logging.info (' option correct ')    else:        logging.error (' must input a digit! ')

Executes if the input is a number, write the info log if it is not written as the error log

Ps:level=logging. DEBUG is representative of the minimum record basic if you change to warning you will not record info,debug information even if it is set

It's written in a file.

Display the log to the screen with output to a file

Import Logginglogger = Logging.getlogger ("Simple_example") logger.setlevel (logging. DEBUG) #on Screench = logging. Streamhandler () Ch.setlevel (logging. WARNING) #into Filefh = logging. Filehandler ("Log2.log") fh.setlevel (logging. DEBUG) formatter = logging. Formatter ("% (asctime) s-% (name) s-% (LevelName) s-% (module) s:  % (message) S") Ch.setformatter (Formatter) Fh.setformatter (Formatter) logger.addhandler (CH) logger.addhandler (FH) logger.debug ("Debug msg ...") Logger.info ("Info msg ...") Logger.warn ("Warn msg ...") Logger.error ("Error msg ...") Logger.critical ("Critical msg ...")

The screen does not output debug information

Python's logging log module

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.