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