Python Base module: Logging module

Source: Internet
Author: User

Function: Format output log
Five levels debug,info,waring,error,critical, default print waring above level input, can be changed.
Default instance is root, do not use default instance when recommended

  Import loggingimport SYS uses a new instance: # Instantiate logging, must be given a name, default is root, not recommended. Logger_instence = Logging.getlogger ("name") # Sets the instance level, which is displayed above info, and the default is waring above. (optionally set) Logger_instence.setlogger (logging.info) # set log format, default format: Log level: Instance Name: Info formatter = logger.setformatter ('% (asctim E) S% (levelname) -8s:% (message) s ') # Specifies a log processing method, such as printing to a screen, storing to a file, and so on, this example is stored to a file File_handler = logging. Filehandler ("Test.log") File_handler.setformatter (Formatter) # can specify output format Setformatter = Formatter # You can also assign a value to File_handler's Formatter property directly # and then create a handlerprint_handler = logging that prints to the screen. Streamhander (sys.stdout) Print_handler.setformatter (Formatter) # Add the specified log processing method to the instance, you can add multiple log processing methods Logger.addhandler ( File_handler) # Responsible for storing to file Logger.addhandler (Print_handler) # is responsible for printing to the screen # call logger, while the screen is displayed , save the log to file Logger.debug ("This is a Debug test") Logger.info ("This is an info test") logger.waring ("This is a Waring test") Logger.error (" This is an error test ") logger.critical (" This is a critical test ")  

About logger, the above is the basic method of use, or is a simple template, more advanced use is required to continue to practice, since not think of the following two-bit write good, understand the following two-bit logging module article.

Https://www.jianshu.com/p/feb86c06c4f4
http://python.jobbole.com/86887/

Python Base module: Logging module

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.