I think it's pretty good. I 've talked about all the basic skills.
The following describes how to use it.CodeIt is easy to write:
1 def initlog ():
2 Import Logging
3
4 logger = logging. getlogger ()
5 hdlr = logging. filehandler (logfile)
6 formatter = logging. formatter ('% (asctime) S % (levelname) S % (Message) s ')
7 hdlr. setformatter (formatter)
8 logger. addhandler (hdlr)
9 logger. setlevel (logging. notset)
10
11 return Logger
This function is used to return a log object.
Line 3 generates a log object with a name, which can be a default value. (Note: The name here is the name of the log class, used to identify this logger class)
Line 3 generates a handler. Logging supports many handler, such as filehandler, sockethandler, and smtphandler. I use filehandler to write files. Logfile is a global variable, which is a file name, such as 'crawl. log '.
Line 3 generates a formatter to standardize the log output format. If this line of code is not available, the default format is: "% (Message) s ". That is, when a log is written, the information is what is in the log, there is no date, there is no information level and other information. Logging supports many replacement values. For details, see the formatter documentation. There are three items: time, information level, and log information. (Three items are commonly used)
The 7th line formatter is set to the processor.
8th rows of processors are added to the log object.
Row 3 sets the log output level. Logging provides multiple levels of log information, such as notset, debug, info, warning, error,Critical. Each level corresponds to a value. If this sentence is not executed, the default value is 30 (warning ). You can run logging. getlevelname (logger. getjavastivelevel () to view the default log level. Log objects provide different functions for output of different levels of information, such as Info (), error (), and debug. When logs are written, information smaller than the specified level is ignored. Therefore, you must set this parameter to output the desired log level. Here I set it to notset (the value is 0), that is, to output all information.
With the log object, the output is very simple:
The following levels are available:
Logger. Error (Message)
Logger.info (Message)
Logger. Warning (MSG)
Logger. Critical (MSG)
Logger. Exception (MSG)
Logger. Log (MSG)
Def initlog (): Import logging logger = logging. getlogger ('loggere18') hdlr = logging. filehandler ("D: \ Euler \ E18 \ log.txt", "W") # Write mode formatter = logging. formatter ('% (asctime) s, % (levelname) s, % (Message) s') hdlr. setformatter (formatter) Logger. addhandler (hdlr) Logger. setlevel (logging. debug) return loggerimport iologger = initlog () logger.info ("logger name is '" + logger. name + "'") F = open ("D: \ Euler \ E18 \ data.txt", "R") logger.info ("Open File") Data = f. readlines () logger.info ("Get Data") F. close () Logger. debug (data) for I in range (0, Len (data)-1): Data [I] = data [I]. strip () Logger. debug (data)
Here, the log uses the W mode and only the write mode, so that you do not need to append the log to increase the trouble. Setlevel must be noted not to use notset, which cannot be written, and no logs can be written. Use debug.
To close the logger class, use the logging. Shutdown () method.