#申明: This article refers to http://www.cnblogs.com/dahu-daqing/p/7040764.html
Logging Module Introduction
# The logging module is a python built-in standard module that is used primarily for output program run logs # you can set the output log level, log save path, log file rollback, etc. # The following advantages than print # 1. You can set different log levels, output only important information, without having to display a lot of debugging information # 2.print outputs all of the information to the standard output, and serious developers see other data from the standard output, while the logging module allows the developer to decide where to export the information and how to output
Basic use of the logging module
ImportLogginglogging.basicconfig ( level=logging.info,#This sentence can not be, really useful is the following logger.setlevel (level)format='% (asctime) s-% (name) s-% (levelname) s-% (message) s') Logger= Logging.getlogger ('Satori') Logger.setlevel (logging.info)#this is set as infoLogger.info ('Ancient Ming Land basin') Logger.debug ('Fleur Lus Carrets') logger.warning ('Ancient Ming Land Love') Logger.info ('MMP')#Run program, console output" "2018-06-05 14:12:20,160-satori-info-Guming basin 2018-06-05 14:12:20,160-satori-warning-Guming ground love 2018-06-05 14:12:20,160 -SATORI-INFO-MMP" "#Many message levels, such as debug,info,warning,error,critical, can be selected in logging. #by giving logger or handler different levels, developers can output only error messages to a specific record file, or only debug information when debugging#For example, we can change the logger level to debugLogging.basicconfig (#level=logging. DEBUG, # Comment out the sentenceformat='% (asctime) s-% (name) s-% (levelname) s-% (message) s') Logger= Logging.getlogger ('Satori') Logger.setlevel (logging. DEBUG)#set here, the discovery does not affect the running results of the programLogger.info ('Ancient Ming Land basin') Logger.debug ('Fleur Lus Carrets') logger.warning ('Ancient Ming Land Love') Logger.info ('MMP')#run the program, output the results" "2018-06-05 14:12:20,160-satori-info-Guming basin 2018-06-05 14:12:20,160-satori-debug-Fleur Lus Carrets 2018-06-05 14:12:20, 160-satori-warning-Guming, 2018-06-05 14:12:20,160-satori-info-mmp" "
Writing a log to a file
#parameters of the Logging.basicconfig function:#FileName: Specifies the log file name;#FileMode: Same as Open function, specify open mode of log file, ' W ' or ' a ';#Format : Specifies the formats and contents of the output, format can output a lot of useful information#Parameters#% (Levelno) S: Print the value of the log level#% (levelname) s: Print the name of the log level#% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]#% (filename) s: Prints the current name of the executing program#% (funcName) s: Print the current function of the log#% (Lineno) d: Print the current line number of the log#% (asctime) s: Time to print logs#% (thread) d: Print thread ID#% (threadname) s: Print thread name#% (process) d: Print process ID#% (message) s: Print log information#datefmt: Specify time format, same as Time.strftime ();#level: Sets the log levels by default to logging. warnning;#stream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneouslyImportLogging#set logging, create a filehandler, set the format of the output message, add it to logger, and then write the log to the specified fileLogger = Logging.getlogger ('Ancient Ming Land basin')#Whatever the name is.Logger.setlevel (Logging.info)#set the log level#using Logging.filehandler, which is like open, creates a Log.txt file, handler as a file descriptor, similar to the F in F=open ()Handler = logging. Filehandler ('Log.txt', mode='a', encoding='Utf-8')#set the log level for a filehandler.setlevel (logging.info)#to create an output format with loggingFormatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s')#Give the format to handler, equal to handler to swallow the format.Handler.setformatter (Formatter)#handler as a file handle, has already set the level, and swallowed the logging set up the formatter format. #at this time the handler is ready, and then let it be logger swallowed, at this time the logger has been perfected, you can start printing logs. Logger.addhandler (handler)#With regard to setting the level, logger is set once, and handler is set again. #set two times does not affect, you can set the two levels are the same, if one party is set too high, even if the other party is not high level, then will not output#you can set the hierarchy to be consistentLogger.info ('Ancient Ming Land basin') Logger.debug ('Fleur Lus Carrets') logger.warning ('Ancient Ming Land Love') Logger.info ('MMP')#Execute the program, you can see there is a TXT file, the contents of the file is" "2018-06-05 14:23:27,767-Guming Basin-INFO-Guming basin 2018-06-05 14:23:27,767-Guming Basin-WARNING-Guming ground love 2018-06-05 14:23:27,767-Ancient Ming Basin-INFO-MMP" "
Output logs to both file and console
#logging Add Streamhandler, you can output the log to the screenImportLogginglogger= Logging.getlogger ('Ancient Ming Land basin') Logger.setlevel ( level=logging.info) Handler= Logging. Filehandler ('Log1.txt','a', encoding='Utf-8') Handler.setlevel (logging.info) Formatter= Logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') Handler.setformatter (formatter)#Output to consoleconsole = logging. Streamhandler ()#because it is output to the console, no parameters are requiredConsole.setlevel (Logging.info)#If you do not specify a format, only normal strings are outputConsole.setformatter (Formatter)#specify format, output by formatLogger.addhandler (console) Logger.addhandler (handler) Logger.info ('Ancient Ming Land basin') Logger.debug ('Fleur Lus Carrets') logger.warning ('Ancient Ming Land Love') Logger.info ('MMP')#output, you can see the TXT file and the console will output the following" "2018-06-05 19:10:11,995-Guming Basin-INFO-Guming basin, 2018-06-05 19:10:11,995-Guming Basin-WARNING-Guming ground love 2018-06-05 19:10:11,995- Gu Ming Basin-info-mmp" "#some of the wrong levels" "FATAL: Fatal error Critical: particularly bad things, such as memory exhaustion, empty disk space, generally rarely use error: When an error occurs, such as an IO operation failure or a connection problem warning: An important event occurs, but not an error, If the user login password error info: Handling requests or state changes, such as Daily transactions debug: Debug Process using the debug level, such as the algorithm in the middle state of each loop" "
Python--logging Module