Python logs are mainly used logging modules;
The sample code is as follows:
#Coding:utf-8ImportLoggingclasslogger ():" "% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s" "format_dict= { 1:logging. Formatter ('% (asctime) s-% (filename) s-[line:% (Lineno) d]-% (name) s-% (levelname) s-% (message) s'), 2:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s'), 3:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s'), 4:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s'), 5:logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') } def __init__(self, logname, loglevel, logger):" "Specify the file path to save the log, the log level, and the call file to deposit the log into the specified file" " #Create a loggerSelf.logger =Logging.getlogger (Logger) self.logger.setLevel (logging. DEBUG)#create a handler to write to the log fileFH =logging. Filehandler (logname) fh.setlevel (logging. DEBUG)#create another handler for output to the consoleCH =logging. Streamhandler () Ch.setlevel (logging. DEBUG)#define the output format of the handler #formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')Formatter =Self.format_dict[int (loglevel)] Fh.setformatter (formatter) ch.setformatter (formatter)#Add handler to loggerSelf.logger.addHandler (FH) self.logger.addHandler (CH)def __init__(Self,loglevel, logger):" "Specify the file path to save the log, the log level, and the call file to deposit the log into the specified file" " #Create a loggerSelf.logger =Logging.getlogger (Logger) self.logger.setLevel (logging. DEBUG)#create another handler for output to the consoleCH =logging. Streamhandler () Ch.setlevel (logging. DEBUG)#define the output format of the handler #formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')Formatter =Self.format_dict[int (loglevel)] Ch.setformatter (formatter)#Add handler to loggerself.logger.addHandler (CH)defGetlog (self):returnSelf.loggerif __name__=='__main__': Logger= Logger (loglevel=2, logger="Log"). Getlog () Logger.info ("We are the world!!!!")
Python's log-related apps