This article is suitable for just contact with the logging module, want to quickly use and see the effect of children's shoes. If you want to fully understand the logging module, please go to
directly on the code + comment
#1. Import Module LoggingImportLogging#2. Create a Logger instanceLogger = Logging.getlogger (__name__)#3. Configure log properties (the level of logs that need to be printed)Logger.setlevel (logging. DEBUG)#4. Create an handler instance (if you want to save the log file to your hard disk)Handler = logging. Filehandler ("Log.txt")#5. Add log Formatformat="% (asctime) s% (name) s% (LevelName) s--% (message) s"Handler.setformatter (logging. Formatter (format))#6. Generate Log FilesLogger.addhandler (handler)#7. Write Log ContentsLogger.info ("Info") Logger.debug ("Debug") logger.warning ("Waring") Logger.info ("Infofofofof")
The contents of the generated log file:
Another: Now the priority of Logging.basicconfig, Logger.setlevel, handler.setlevel a bit vague, welcome to Exchange ~
Declaration: Original article, welcome reprint, Reprint please indicate the source http://www.cnblogs.com/kaerxifa/p/8693661.html
Primary use of the Python-logging module