Python's log-related apps

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.